package cn.yunyichina.provider.sb.service.impl;

import cn.yunyichina.provider.framework.annotation.validate.ValidateException;
import cn.yunyichina.provider.framework.annotation.validate.Validator;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.hisiface.shangbao.*;
import cn.yunyichina.provider.sb.constant.Constant;
import cn.yunyichina.provider.sb.dao.*;
import cn.yunyichina.provider.sb.entity.*;
import cn.yunyichina.provider.sb.util.Util;
import cn.yunyichina.provider.sb.vo.*;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.AlipayEncrypt;
import com.alipay.api.response.AlipayCommerceDataSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 15-AK on 2017/7/16.
 */
@Service
public class NotifyServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(NotifyServiceImpl.class);
    @Autowired
    private MedicalPatientDao medicalPatientDao;
    @Autowired
    private MedicalOutpatientDiagnosisDao medicalOutpatientDiagnosisDao;
    @Autowired
    private MedicalCheckingDao medicalCheckingDao;
    @Autowired
    private MedicalCheckingDetailDao medicalCheckingDetailDao;
    @Autowired
    private MedicalOutpatientFeeDao medicalOutpatientFeeDao;
    @Autowired
    private MedicalOutpatientFeeDetailDao medicalOutpatientFeeDetailDao;
    @Autowired
    private MedicalExaminationDao medicalExaminationDao;
    @Autowired
    private MedicalOperationRecordDao medicalOperationRecordDao;
    @Autowired
    private MedicalInhospitalDiagnosisDao medicalInhospitalDiagnosisDao;
    @Autowired
    private MedicalInhospitalFeeDao medicalInhospitalFeeDao;
    @Autowired
    private MedicalInhospitalFeeDetailDao medicalInhospitalFeeDetailDao;
    @Autowired
    private MedicalInhospitalReportidsDao medicalInhospitalReportidsDao;
    @Autowired
    private MedicalInvoiceNoDao medicalInvoiceNoDao;
    private final static String HOSPITAL_CODE = "szszyy";

    /**
     * 支付宝回调通知及重传
     *
     * @param data
     * @return
     */
    public ResponseObject alipayNotify(String data) {
        logger.info("支付宝回调入参data={}", data);
        ResponseObject responseObject = new ResponseObject();
        try {
            JSONObject jsonObject = JsonUtils.parseObject(data, JSONObject.class);
            String content = jsonObject.getString("content");
            //解密返回回调回来的参数
            String decryptContent = AlipayEncrypt.decryptContent(content, "AES", Constant.ENCRYPT_KEY, "utf8");
            logger.info("解密支付宝回调的数据 decryptContent ={}", decryptContent);
            NotifyRequest notifyRequest = JsonUtils.parseObject(decryptContent, NotifyRequest.class);

            List<SenceType> senceTypeList = notifyRequest.getSceneTypeList();
            if (CollectionUtils.isEmpty(senceTypeList)) {
                MedicalPatient queryMedicalPatient = new MedicalPatient();
                queryMedicalPatient.setPatId(notifyRequest.getPatientId());
                queryMedicalPatient.setVisitTime(notifyRequest.getVisitDate());
                queryMedicalPatient.setHospitalId(notifyRequest.getHospitalId());
                if ("O".equals(notifyRequest.getMedicalType())) {
                    //门诊业务
                    queryMedicalPatient.setMzFeeId(notifyRequest.getSerialNo());
                    queryMedicalPatient.setBusinessType(new Short("1"));
                } else {
                    //住院业务
                    queryMedicalPatient.setInpatientId(notifyRequest.getSerialNo());
                    queryMedicalPatient.setBusinessType(new Short("2"));
                }

                List<MedicalPatient> medicalPatientList = medicalPatientDao.select(queryMedicalPatient);
                if (CollectionUtils.isNotEmpty(medicalPatientList)) {
                    MedicalPatient medicalPatient = medicalPatientList.get(0);
                    //更新报案号
                    medicalPatient.setNotifyFlag(new Short("1"));
                    medicalPatient.setCaseNo(notifyRequest.getCaseNo());
                    medicalPatientDao.update(medicalPatient);
                    //住院的业务需要在支付回调的时候拉取入院诊断信息
                    if (medicalPatient.getBusinessType() == 2 && medicalPatient.getDownLoadInHospitalDiagnosisFlag() == 0) {
                        getInHospitalDiagnosisInfo(medicalPatient);
                    }
                } else {
                    responseObject.setResultCodeAndMessage("1001", "患者不存在");
                }
            } else {

                for (SenceType item : senceTypeList) {
                    if (Constant.MEDICAL_OUTPATIENT_DIAGNOSIS.equals(item.getSceneType())) {
                        List<MedicalOutpatientDiagnosis> medicalOutpatientDiagnosisList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalOutpatientDiagnosis queryMedicalOutpatientDiagnosis = new MedicalOutpatientDiagnosis();
                            queryMedicalOutpatientDiagnosis.setHospitalCode(notifyRequest.getHospitalId());
                            queryMedicalOutpatientDiagnosis.setCaseNo(notifyRequest.getCaseNo());
                            medicalOutpatientDiagnosisList = medicalOutpatientDiagnosisDao.select(queryMedicalOutpatientDiagnosis);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalOutpatientDiagnosisList = medicalOutpatientDiagnosisDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalOutpatientDiagnosisList)) {
                            executeUploadMedicalOutpatientDiagnosis(medicalOutpatientDiagnosisList);
                        }
                    } else if (Constant.MEDICAL_OUTPATIENT_FEE.equals(item.getSceneType())) {
                        List<MedicalOutpatientFee> medicalOutpatientFeeList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalOutpatientFee queryMedicalOutpatientFee = new MedicalOutpatientFee();
                            queryMedicalOutpatientFee.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalOutpatientFee.setCaseNo(notifyRequest.getCaseNo());
                            medicalOutpatientFeeList = medicalOutpatientFeeDao.select(queryMedicalOutpatientFee);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalOutpatientFeeList = medicalOutpatientFeeDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalOutpatientFeeList)) {
                            executeUploadMedicalOutPatientFeeMessage(medicalOutpatientFeeList);
                        }
                    } else if (Constant.MEDICAL_OUTPATIENT_FEE_DETAIL.equals(item.getSceneType())) {
                        List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalOutpatientFeeDetail queryMedicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
                            queryMedicalOutpatientFeeDetail.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalOutpatientFeeDetail.setCaseNo(notifyRequest.getCaseNo());
                            medicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.select(queryMedicalOutpatientFeeDetail);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalOutpatientFeeDetailList)) {
                            executeUploadMedicalOutPatientFeeDetailMessage(medicalOutpatientFeeDetailList);
                        }
                    } else if (Constant.MEDICAL_OUTPATIENT_EXAMINATION.equals(item.getSceneType()) || Constant.MEDICAL_INHOSPITAL_EXAMINATION.equals(item.getSceneType())) {
                        List<MedicalExamination> medicalExaminationList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalExamination queryMedicalExamination = new MedicalExamination();
                            queryMedicalExamination.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalExamination.setCaseNo(notifyRequest.getCaseNo());
                            medicalExaminationList = medicalExaminationDao.select(queryMedicalExamination);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalExaminationList = medicalExaminationDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalExaminationList) && "O".equals(notifyRequest.getMedicalType())) {
                            executeUploadExaminationMessage(medicalExaminationList, item.getSceneType());
                        }

                        if (CollectionUtils.isNotEmpty(medicalExaminationList) && "I".equals(notifyRequest.getMedicalType())) {
                            //// TODO: 2017/11/9
                            executeUploadInhospitalExaminationMessage(medicalExaminationList, item.getSceneType());
                        }

                    } else if (Constant.MEDICAL_OUTPATIENT_CHECKING.equals(item.getSceneType()) || Constant.MEDICAL_INHOSPITAL_CHECKING.equals(item.getSceneType())) {
                        List<MedicalChecking> medicalCheckingList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalChecking queryMedicalChecking = new MedicalChecking();
                            queryMedicalChecking.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalChecking.setCaseNo(notifyRequest.getCaseNo());
                            medicalCheckingList = medicalCheckingDao.select(queryMedicalChecking);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalCheckingList = medicalCheckingDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalCheckingList) && "O".equals(notifyRequest.getMedicalType())) {
                            executeUploadCheckingMessage(medicalCheckingList, item.getSceneType());
                        }
                        if (CollectionUtils.isNotEmpty(medicalCheckingList) && "I".equals(notifyRequest.getMedicalType())) {
                            executeUploadInhospitalCheckingMessage(medicalCheckingList, item.getSceneType());
                        }
                    } else if (Constant.MEDICAL_OUTPATIENT_CHECKING_DETAIL.equals(item.getSceneType()) || Constant.MEDICAL_INHOSPITAL_CHECKING_DETAIL.equals(item.getSceneType())) {
                        List<MedicalCheckingDetail> medicalCheckingDetailList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalCheckingDetail queryMedicalCheckingDetail = new MedicalCheckingDetail();
                            queryMedicalCheckingDetail.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalCheckingDetail.setCaseNo(notifyRequest.getCaseNo());
                            medicalCheckingDetailList = medicalCheckingDetailDao.select(queryMedicalCheckingDetail);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalCheckingDetailList = medicalCheckingDetailDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalCheckingDetailList) && "O".equals(notifyRequest.getMedicalType())) {
                            executeUploadCheckingDetailMessage(medicalCheckingDetailList, item.getSceneType());
                        }

                        if (CollectionUtils.isNotEmpty(medicalCheckingDetailList) && "I".equals(notifyRequest.getMedicalType())) {
                            executeUploadInhospitalCheckingDetailMessage(medicalCheckingDetailList, item.getSceneType());
                        }

                    } else if (Constant.MEDICAL_OUTPATIENT_OPERATION_RECORD.equals(item.getSceneType()) || Constant.MEDICAL_INHOSPITAL_OPERATION_RECORD.equals(item.getSceneType())) {
                        List<MedicalOperationRecord> medicalOperationRecordList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalOperationRecord queryMedicalOperationRecord = new MedicalOperationRecord();
                            queryMedicalOperationRecord.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalOperationRecord.setCaseNo(notifyRequest.getCaseNo());
                            medicalOperationRecordList = medicalOperationRecordDao.select(queryMedicalOperationRecord);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalOperationRecordList = medicalOperationRecordDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalOperationRecordList) && "O".equals(notifyRequest.getMedicalType())) {
                            executeUploadSurgeryMessage(medicalOperationRecordList, item.getSceneType());
                        }
                        if (CollectionUtils.isNotEmpty(medicalOperationRecordList) && "I".equals(notifyRequest.getMedicalType())) {
                            executeUploadInhospitalSurgeryMessage(medicalOperationRecordList, item.getSceneType());
                        }
                    } else if (Constant.MEDICAL_INHOSPITAL_DIAGNOSIS.equals(item.getSceneType())) {
                        List<MedicalInhospitalDiagnosis> medicalInhospitalDiagnosisList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalInhospitalDiagnosis queryMedicalInhospitalDiagnosis = new MedicalInhospitalDiagnosis();
                            queryMedicalInhospitalDiagnosis.setHospitalCode(notifyRequest.getHospitalId());
                            queryMedicalInhospitalDiagnosis.setCaseNo(notifyRequest.getCaseNo());
                            medicalInhospitalDiagnosisList = medicalInhospitalDiagnosisDao.select(queryMedicalInhospitalDiagnosis);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalInhospitalDiagnosisList = medicalInhospitalDiagnosisDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalInhospitalDiagnosisList)) {
                            executeUploadInhospitalDiagnosis(medicalInhospitalDiagnosisList);
                        }
                    } else if (Constant.MEDICAL_INHOSPITAL_FEE.equals(item.getSceneType())) {
                        List<MedicalInhospitalFee> medicalInhospitalFeeList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalInhospitalFee queryMedicalInhospitalFee = new MedicalInhospitalFee();
                            queryMedicalInhospitalFee.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalInhospitalFee.setCaseNo(notifyRequest.getCaseNo());
                            medicalInhospitalFeeList = medicalInhospitalFeeDao.select(queryMedicalInhospitalFee);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalInhospitalFeeList = medicalInhospitalFeeDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalInhospitalFeeList)) {
                            executeUploadMedicalInhospitalFeeMessage(medicalInhospitalFeeList);
                        }
                    } else if (Constant.MEDICAL_INHOSPITAL_FEE_DETAIL.equals(item.getSceneType())) {
                        List<MedicalInhospitalFeeDetail> medicalInhospitalFeeDetailList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalInhospitalFeeDetail queryMedicalInhospitalFeeDetail = new MedicalInhospitalFeeDetail();
                            queryMedicalInhospitalFeeDetail.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalInhospitalFeeDetail.setCaseNo(notifyRequest.getCaseNo());
                            medicalInhospitalFeeDetailList = medicalInhospitalFeeDetailDao.select(queryMedicalInhospitalFeeDetail);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalInhospitalFeeDetailList = medicalInhospitalFeeDetailDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalInhospitalFeeDetailList)) {
                            executeUploadMedicalInhospitalFeeDetailMessage(medicalInhospitalFeeDetailList);
                        }
                    } else if (Constant.MEDICAL_INVOICE_SUPPLY.equals(item.getSceneType())) {
                        List<MedicalInvoiceNo> medicalInvoiceNoList = new ArrayList<>();
                        if (StringUtils.isBlank(item.getOffset())) {
                            MedicalInvoiceNo queryMedicalInvoiceNo = new MedicalInvoiceNo();
                            queryMedicalInvoiceNo.setOrgCode(notifyRequest.getHospitalId());
                            queryMedicalInvoiceNo.setCaseNo(notifyRequest.getCaseNo());
                            medicalInvoiceNoList = medicalInvoiceNoDao.select(queryMedicalInvoiceNo);
                        } else {
                            List<String> offSetList = Arrays.asList(item.getOffset().split(","));
                            medicalInvoiceNoList = medicalInvoiceNoDao.selectByOffset(notifyRequest.getCaseNo(), offSetList);
                        }

                        if (CollectionUtils.isNotEmpty(medicalInvoiceNoList)) {
                            executeUploadMedicalInvoiceNoMessage(medicalInvoiceNoList);
                        }
                    }
                }
            }
            responseObject.setResultCodeAndMessage("0", "成功");
        } catch (Exception e) {
            responseObject.setResultCodeAndMessage("-1", "失败");
            logger.error("支付宝回调异常", e);
        }
        return responseObject;
    }

    //获取入院诊断信息在支付宝回调的时候获取
    public void getInHospitalDiagnosisInfo(MedicalPatient medicalPatient) {
        try {
            logger.info("inPatientId={},入院就诊人信息 medicalPatient ={}", new Object[]{medicalPatient.getInpatientId(),JsonUtils.toJsonString(medicalPatient)});
            SBInPatientDiagnosisVo inPatientDiagnosisVo = new SBInPatientDiagnosisVo();
            if (StringUtils.isNotEmpty(medicalPatient.getInpatientId())) {
                inPatientDiagnosisVo.setInPatientId(medicalPatient.getInpatientId());
            } else {
                inPatientDiagnosisVo.setAdmissionNo(medicalPatient.getAdmissionNo());
                inPatientDiagnosisVo.setInTime(medicalPatient.getInTime() == null ? null : medicalPatient.getInTime().toString());
            }
            inPatientDiagnosisVo.setHospitalCode(medicalPatient.getOrgCode());
            logger.info("inPatientId={},获取入院商保住院诊疗信息his入参inPatientDiagnosisVo={}", new Object[]{medicalPatient.getInpatientId(),JsonUtils.toJsonString(inPatientDiagnosisVo)});
            ResCommon<SBInPatientDiagnosisVoResponse> hisfiaceDiagnosisResponse = HisifaceService.getHisifaceClient().getSBInPatientDiagnosis(inPatientDiagnosisVo);
            logger.info("inPatientId={},获取入院商保住院诊疗信息his出参 hisfiaceDiagnosisResponse ={}", new Object[]{medicalPatient.getInpatientId(),JsonUtils.toJsonString(hisfiaceDiagnosisResponse)});
            if (!"0".equals(hisfiaceDiagnosisResponse.getResultCode())) {
                medicalPatient.setDownLoadInHospitalDiagnosisExceptionMessage(hisfiaceDiagnosisResponse.getResultMessage());
                medicalPatient.setDownLoadInHospitalDiagnosisTimes(medicalPatient.getDownLoadInHospitalDiagnosisTimes() + 1);
                medicalPatientDao.update(medicalPatient);
            } else {
                SBInPatientDiagnosisVoResponse sbInPatientDiagnosisVoResponse = hisfiaceDiagnosisResponse.getResult();
                if (sbInPatientDiagnosisVoResponse != null) {
                    MedicalInhospitalDiagnosis medicalInhospitalDiagnosis = buildMedicalInhospitalDiagnosis(sbInPatientDiagnosisVoResponse);
                    medicalInhospitalDiagnosis.setTotalRows(2);
                    medicalInhospitalDiagnosis.setCurrentOffset(1);
                    medicalInhospitalDiagnosis.setOrgCode(medicalPatient.getOrgCode());
                    medicalInhospitalDiagnosis.setCaseNo(medicalPatient.getCaseNo());
                    medicalInhospitalDiagnosis.setMedicalPatientId(medicalPatient.getId());
                    logger.info("inPatientId={},入院商保住院诊疗信息入库",medicalPatient.getInpatientId());
                    medicalInhospitalDiagnosisDao.insert(medicalInhospitalDiagnosis);
                    logger.info("inPatientId={},入院商保住院诊疗信息入库后更新就诊人信息状态",medicalPatient.getInpatientId());
                    medicalPatient.setDownLoadInHospitalDiagnosisTimes(medicalPatient.getDownLoadInHospitalDiagnosisTimes() + 1);
                    medicalPatient.setDownLoadInHospitalDiagnosisFlag(new Short("1"));
                    medicalPatientDao.update(medicalPatient);
                }
            }
        } catch (Exception e) {
            logger.info("获取商保住院诊疗信息系统异常", e);
        }
    }


    /**
     * 出院通知
     *
     * @param data
     * @return
     */
    public ResponseObject reportNotifyOutHospital(String data) {
        logger.info("出院通知原始入参data={}", data);
        ResponseObject responseObject = new ResponseObject();
        try {
            JSONObject jsonObject = JsonUtils.parseObject(data, JSONObject.class);
            String hospitalCode = jsonObject.getString("hospital_code");
            String inPatientId = jsonObject.getString("in_patient_id");
            String admissionNo = jsonObject.getString("admission_no");
            Integer inTime = jsonObject.getInteger("in_time");
            if (StringUtils.isBlank(hospitalCode)) {
                responseObject.setResultCodeAndMessage("1001", "参数缺失请检查");
                return responseObject;
            }

            if (StringUtils.isBlank(inPatientId)) {
                if (StringUtils.isBlank(admissionNo) || inTime == null) {
                    responseObject.setResultCodeAndMessage("1001", "参数缺失请检查");
                    return responseObject;
                }
            }
            MedicalPatient medicalPatient = new MedicalPatient();
            medicalPatient.setBusinessType(new Short("2"));
            medicalPatient.setOutHospitalNotityFlag(new Short("1"));
            medicalPatient.setInpatientId(inPatientId);
            medicalPatient.setOrgCode(hospitalCode);
            medicalPatientDao.updateOutHospitalNotify(medicalPatient);
            responseObject.setResultCodeAndMessage("0","成功");
        } catch (Exception e) {
            logger.error("出院回调通知异常", e);
            responseObject.setResultCodeAndMessage("19999", "参数缺失");
        }
        return responseObject;
    }



//    public ResponseObject reportNotifyOutHospital(String data) {
//        logger.info("出院通知原始入参data={}", data);
//        ResponseObject responseObject = new ResponseObject();
//        try {
//            JSONObject jsonObject = JsonUtils.parseObject(data, JSONObject.class);
//            String hospitalCode = jsonObject.getString("hospital_code");
//            String inPatientId = jsonObject.getString("in_patient_id");
//            String admissionNo = jsonObject.getString("admission_no");
//            Integer inTime = jsonObject.getInteger("in_time");
//            if (StringUtils.isBlank(hospitalCode)) {
//                responseObject.setResultCodeAndMessage("1001", "参数缺失请检查");
//                return responseObject;
//            }
//
//            if (StringUtils.isBlank(inPatientId)) {
//                if (StringUtils.isBlank(admissionNo) || inTime == null) {
//                    responseObject.setResultCodeAndMessage("1001", "参数缺失请检查");
//                    return responseObject;
//                }
//            }
//
//            //his通知出院后获取出院诊断信息
//            SBInPatientDiagnosisVo inPatientDiagnosisVo = new SBInPatientDiagnosisVo();
//            inPatientDiagnosisVo.setHospitalCode(hospitalCode);
//            if (StringUtils.isNotEmpty(inPatientId)) {
//                inPatientDiagnosisVo.setInPatientId(inPatientId);
//            } else {
//                inPatientDiagnosisVo.setAdmissionNo(admissionNo);
//                inPatientDiagnosisVo.setInTime(inTime.toString());
//            }
//
//            responseObject = getOutHospitalDiagnosisInfo(inPatientDiagnosisVo);
//            if (!"0".equals(responseObject.getResultCode())) {
//                return responseObject;
//            }
//            //his通知出院后获取费用总清单信息
//            InPatientSumFeeListVo inPatientSumFeeListVo = new InPatientSumFeeListVo();
//            inPatientSumFeeListVo.setHospitalCode(hospitalCode);
//            if (StringUtils.isNotEmpty(inPatientId)) {
//                inPatientSumFeeListVo.setInPatientId(inPatientId);
//            } else {
//                inPatientSumFeeListVo.setAdmissionNo(admissionNo);
//                inPatientSumFeeListVo.setInTime(inTime == null ? null : inTime.toString());
//            }
//
//            responseObject = getInhospitalFeeInfo(inPatientSumFeeListVo);
//            if (!"0".equals(responseObject.getResultCode())) {
//                return responseObject;
//            }
//
//            //获取检验检查手术记录reportIds
//            SBInPatientReportIdsVo sbInPatientReportIdsVo = new SBInPatientReportIdsVo();
//            sbInPatientReportIdsVo.setHospitalCode(hospitalCode);
//            if (StringUtils.isNotEmpty(inPatientId)) {
//                sbInPatientReportIdsVo.setInPatientId(inPatientId);
//            } else {
//                sbInPatientReportIdsVo.setAdmissionNo(admissionNo);
//                sbInPatientReportIdsVo.setInTime(inTime == null ? null : inTime.toString());
//            }
//            return getSBInPatientReportIds(sbInPatientReportIdsVo);
//
//        } catch (Exception e) {
//            logger.error("出院回调通知异常", e);
//            responseObject.setResultCodeAndMessage("19999", "参数缺失");
//        }
//        return responseObject;
//    }

    /**
     * 获取住院的报告ids
     *
     * @param sbInPatientReportIdsVo
     * @return
     */
    public ResponseObject getSBInPatientReportIds(SBInPatientReportIdsVo sbInPatientReportIdsVo) {
        ResponseObject responseObject = new ResponseObject();
        try {
            MedicalPatient queryMedicalPatient = new MedicalPatient();
            if (StringUtils.isNotEmpty(sbInPatientReportIdsVo.getInPatientId())) {
                queryMedicalPatient.setInpatientId(sbInPatientReportIdsVo.getInPatientId());
            } else {
                queryMedicalPatient.setAdmissionNo(sbInPatientReportIdsVo.getAdmissionNo());
                queryMedicalPatient.setInTime(converterStringToInt(sbInPatientReportIdsVo.getInTime()));
            }
            queryMedicalPatient.setBusinessType(new Short("2"));
            List<MedicalPatient> medicalPatientList = medicalPatientDao.select(queryMedicalPatient);
            logger.info("获取住院检验或检查或手术记录reportId 的就诊人信息 medicalPatientList={}", new Object[]{JsonUtils.toJsonString(medicalPatientList)});
            if (CollectionUtils.isEmpty(medicalPatientList)) {
                responseObject.setResultCodeAndMessage("1001", "无法找到对应患者信息");
                return responseObject;
            }
            MedicalPatient medicalPatient = medicalPatientList.get(0);

            MedicalInhospitalReportids queryMedicalInhospitalReportids = new MedicalInhospitalReportids();
            queryMedicalInhospitalReportids.setMedicalPatientId(medicalPatient.getId());
            List<MedicalInhospitalReportids> medicalInhospitalReportidses = medicalInhospitalReportidsDao.select(queryMedicalInhospitalReportids);
            if (CollectionUtils.isNotEmpty(medicalInhospitalReportidses)) {
                responseObject.setResultCodeAndMessage("0", "成功");
                return responseObject;
            }
            logger.info("inPatientId={},请求调度中心获取住院检验和检查reportIds入参={}", new Object[]{sbInPatientReportIdsVo.getInPatientId(),JsonUtils.toJsonString(sbInPatientReportIdsVo)});
            ResCommon<SBInPatientReportIdsVoResponse> hisSBInPatientReportIdsVoResponse = HisifaceService.getHisifaceClient().getSBInPatientReportIds(sbInPatientReportIdsVo);
            logger.info("inPatientId={},请求调度中心获取住院检验和检查reportIds出参={}", new Object[]{sbInPatientReportIdsVo.getInPatientId(),JsonUtils.toJsonString(hisSBInPatientReportIdsVoResponse)});
            if (!"0".equals(hisSBInPatientReportIdsVoResponse.getResultCode())) {
                responseObject.setResultCodeAndMessage("1001", hisSBInPatientReportIdsVoResponse.getResultMessage());
                medicalPatient.setDownLoadReportIdTimes(medicalPatient.getDownLoadReportIdTimes() + 1);
                medicalPatientDao.update(medicalPatient);
                return responseObject;
            }

            SBInPatientReportIdsVoResponse sBInPatientReportIdsVoResponse = hisSBInPatientReportIdsVoResponse.getResult();
            if (sBInPatientReportIdsVoResponse == null) {
                medicalPatient.setDownLoadReportIdFlag(new Short("1"));
                medicalPatient.setDownLoadReportIdTimes(medicalPatient.getDownLoadReportIdTimes() + 1);
                medicalPatientDao.update(medicalPatient);
                responseObject.setResultCodeAndMessage("1001", "没有查询到reportIds");
                return responseObject;
            }

            List<InPatientReportDetail> reportItems = sBInPatientReportIdsVoResponse.getReportItems();
            List<MedicalInhospitalReportids> medicalInhospitalReportidsList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(reportItems)) {
                int offset = 1;
                for (InPatientReportDetail reportItem : reportItems) {
                    MedicalInhospitalReportids medicalInhospitalReportids = new MedicalInhospitalReportids();
                    medicalInhospitalReportids.setId(PKGenerator.generateId());
                    medicalInhospitalReportids.setMedicalPatientId(medicalPatient.getId());
                    medicalInhospitalReportids.setAdmissionNo(medicalPatient.getAdmissionNo());
                    medicalInhospitalReportids.setInTime(medicalPatient.getInTime());
                    medicalInhospitalReportids.setInpatientId(medicalPatient.getInpatientId());
                    medicalInhospitalReportids.setCaseNo(medicalPatient.getCaseNo());
                    medicalInhospitalReportids.setOrgCode(medicalPatient.getOrgCode());
                    medicalInhospitalReportids.setReportId(reportItem.getReportId());
                    medicalInhospitalReportids.setReportType(converterStringToShort(reportItem.getReportType()));
                    medicalInhospitalReportids.setCurrentOffset(offset++);
                    medicalInhospitalReportids.setTotalRows(reportItems.size());
                    medicalInhospitalReportids.setDownLoadFlag(new Short("0"));
                    medicalInhospitalReportids.setDownLoadTimes(0);
                    medicalInhospitalReportids.setUploadFlag(new Short("0"));
                    medicalInhospitalReportids.setUploadTimes(0);
                    medicalInhospitalReportids.setCreatedTime(DateUtils.dateToString(new Date()));
                    medicalInhospitalReportidsList.add(medicalInhospitalReportids);

                }

                if (CollectionUtils.isNotEmpty(medicalInhospitalReportidsList)) {
                    medicalInhospitalReportidsDao.batchInsert(medicalInhospitalReportidsList);
                    medicalPatient.setDownLoadReportIdFlag(new Short("1"));
                    medicalPatient.setDownLoadReportIdTimes(medicalPatient.getDownLoadReportIdTimes() + 1);
                    medicalPatientDao.update(medicalPatient);
                }
            }


            if (CollectionUtils.isNotEmpty(medicalInhospitalReportidsList)) {
                for (MedicalInhospitalReportids medicalInhospitalReportids : medicalInhospitalReportidsList) {
                    if (1 == medicalInhospitalReportids.getReportType()) {
                        //1：检查报告
                        SBCheckRecordVo checkRecordVo = new SBCheckRecordVo();
                        checkRecordVo.setHospitalCode(medicalInhospitalReportids.getOrgCode());
                        checkRecordVo.setCheckId(medicalInhospitalReportids.getReportId());
                        logger.info("获取住院商保检查信息his入参 checkRecordVo ={}", new Object[]{JsonUtils.toJsonString(checkRecordVo)});
                        ResCommon<SBCheckRecordVoResponse> hisfiaceSBCheckRecordResponse = HisifaceService.getHisifaceClient().getSBCheckRecord(checkRecordVo);
                        logger.info("获取住院商保检查信息his出参 hisfiaceSBCheckRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBCheckRecordResponse)});
                        if (!"0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
                            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getUploadTimes() + 1);
                            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBCheckRecordResponse.getResultMessage());
                            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                        }

                        if ("0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
                            SBCheckRecordVoResponse sbCheckRecordVoResponse = hisfiaceSBCheckRecordResponse.getResult();
                            if (sbCheckRecordVoResponse != null) {
                                MedicalExamination examination = buildExamination(sbCheckRecordVoResponse);
                                examination.setOrgCode(medicalPatient.getOrgCode());
                                examination.setMedicalPatientId(medicalPatient.getId());
                                examination.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.getId());
                                examination.setCaseNo(medicalPatient.getCaseNo());

                                medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
                                medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
                                medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                                medicalExaminationDao.insert(examination);
                            }
                        }
                    }

                    if (2 == medicalInhospitalReportids.getReportType()) {
                        //通过reportId 查找对应的检验
                        MedicalChecking medicalChecking = null;
                        List<MedicalCheckingDetail> medicalCheckingDetails = null;
                        SBInspectRecordVo inspectRecordVo = new SBInspectRecordVo();
                        inspectRecordVo.setInspectId(medicalInhospitalReportids.getReportId());
                        inspectRecordVo.setHospitalCode(medicalInhospitalReportids.getOrgCode());
                        //拿到检验
                        logger.info("获取商保检验信息his入参 inspectRecordVo ={}", new Object[]{JsonUtils.toJsonString(inspectRecordVo)});
                        ResCommon<SBInspectRecordVoResponse> hisfiaceSBInspectRecordResponse = HisifaceService.getHisifaceClient().getSBInspectRecord(inspectRecordVo);
                        logger.info("获取商保检验信息his出参 hisfiaceSBInspectRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBInspectRecordResponse)});
                        if (!"0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
                            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getUploadTimes() + 1);
                            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBInspectRecordResponse.getResultMessage());
                            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                        }

                        if ("0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
                            SBInspectRecordVoResponse sBInspectRecordVoResponse = hisfiaceSBInspectRecordResponse.getResult();
                            if (sBInspectRecordVoResponse != null) {
                                medicalChecking = buildMedicalChecking(sBInspectRecordVoResponse);
                                medicalChecking.setMedicalPatientId(medicalPatient.getId());
                                medicalChecking.setCaseNo(medicalPatient.getCaseNo());
                                medicalChecking.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.getId());

                                //患者信息id
                                List<SBInspectDetail> inspectDetails = sBInspectRecordVoResponse.getInspectDetails();
                                if (CollectionUtils.isNotEmpty(inspectDetails)) {
                                    medicalCheckingDetails = new ArrayList<>();
                                    for (SBInspectDetail sBInspectDetail : inspectDetails) {
                                        MedicalCheckingDetail medicalCheckingDetail = buildMedicalCheckingDetail(sBInspectDetail);
                                        medicalCheckingDetail.setBusinessFeeId(medicalChecking.getBusinessFeeId());
                                        medicalCheckingDetail.setInspectId(medicalChecking.getInspectId());
                                        medicalCheckingDetail.setBusinessType(converterStringToShort(sBInspectRecordVoResponse.getBusinessType()));
                                        medicalCheckingDetail.setOrgCode(medicalChecking.getOrgCode());
                                        medicalCheckingDetail.setMedicalCheckingId(medicalChecking.getId());
                                        medicalCheckingDetail.setMedicalPatientId(medicalChecking.getMedicalPatientId());
                                        medicalCheckingDetail.setCaseNo(medicalChecking.getCaseNo());
                                        medicalCheckingDetails.add(medicalCheckingDetail);
                                    }
                                }
                                medicalCheckingDao.insert(medicalChecking);
                                if (CollectionUtils.isNotEmpty(medicalCheckingDetails)) {
                                    medicalCheckingDetailDao.batchInsert(medicalCheckingDetails);
                                }
                                medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
                                medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
                                medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                            }
                        }
                    }

                    if (3 == medicalInhospitalReportids.getReportType()) {
                        //3:手术报告
                        SBSurgeryRecordVo surgeryRecordVo = new SBSurgeryRecordVo();
                        surgeryRecordVo.setBusinessType("2");
                        surgeryRecordVo.setHospitalCode(medicalPatient.getOrgCode());
                        surgeryRecordVo.setSurgeryId(medicalInhospitalReportids.getReportId());
                        logger.info("获取商保住院手术记录his入参 surgeryRecordVo ={}", new Object[]{JsonUtils.toJsonString(surgeryRecordVo)});
                        ResCommon<SBSurgeryRecordVoResponse> hisfiaceSBSurgeryRecordVoResponse = HisifaceService.getHisifaceClient().getSBSurgeryRecord(surgeryRecordVo);
                        logger.info("获取商保住院手术记录his出参 hisfiaceSBSurgeryRecordVoResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBSurgeryRecordVoResponse)});
                        if (!"0".equals(hisfiaceSBSurgeryRecordVoResponse.getResultCode())) {
                            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getUploadTimes() + 1);
                            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
                            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                        }
                        if ("0".equals(hisfiaceSBSurgeryRecordVoResponse.getResultCode())) {
                            SBSurgeryRecordVoResponse sbSurgeryRecordVoResponse = hisfiaceSBSurgeryRecordVoResponse.getResult();
                            if (sbSurgeryRecordVoResponse != null) {
                                MedicalOperationRecord medicalOperationRecord = buildMedicalOperationRecord(sbSurgeryRecordVoResponse);

                                medicalOperationRecord.setMedicalPatientId(medicalInhospitalReportids.getMedicalPatientId());
                                medicalOperationRecord.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.getId());
                                medicalOperationRecord.setCaseNo(medicalPatient.getCaseNo());
                                medicalOperationRecordDao.insert(medicalOperationRecord);
                                medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
                                medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
                                medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.info("获取住院检验或检查或手术记录reportId系统异常", e);
            responseObject.setResultCodeAndMessage("-1", "获取住院检验或检查或手术记录reportId系统异常");
            return responseObject;
        }

        return responseObject;
    }

    public ResponseObject getOutHospitalDiagnosisInfo(SBInPatientDiagnosisVo inPatientDiagnosisVo) {
        ResponseObject responseObject = new ResponseObject();
        try {
            MedicalPatient queryMedicalPatient = new MedicalPatient();
            if (StringUtils.isNotEmpty(inPatientDiagnosisVo.getInPatientId())) {
                queryMedicalPatient.setInpatientId(inPatientDiagnosisVo.getInPatientId());
            } else {
                queryMedicalPatient.setAdmissionNo(inPatientDiagnosisVo.getAdmissionNo());
                queryMedicalPatient.setInTime(Integer.valueOf(inPatientDiagnosisVo.getInTime()));
            }

            List<MedicalPatient> medicalPatientList = medicalPatientDao.select(queryMedicalPatient);
            logger.info("获取商保住院诊疗信息出院就诊人信息 medicalPatientList ={}", new Object[]{JsonUtils.toJsonString(medicalPatientList)});
            if (CollectionUtils.isEmpty(medicalPatientList)) {
                responseObject.setResultCodeAndMessage("1001", "无法找到对应患者信息");
                return responseObject;
            }
            MedicalPatient medicalPatient = medicalPatientList.get(0);
            if (medicalPatient.getDownLoadDiagnosisFlag() == 1) {
                logger.info("已经通知过出院信息");
                responseObject.setResultCodeAndMessage("0", "出院已经通知");
                return responseObject;
            }
            logger.info("inPatientId={},获取商保住院诊疗信息his入参inPatientDiagnosisVo={}", new Object[]{inPatientDiagnosisVo.getInPatientId(),JsonUtils.toJsonString(inPatientDiagnosisVo)});
            ResCommon<SBInPatientDiagnosisVoResponse> hisfiaceDiagnosisResponse = HisifaceService.getHisifaceClient().getSBInPatientDiagnosis(inPatientDiagnosisVo);
            logger.info("inPatientId={},获取商保住院诊疗信息his出参 hisfiaceDiagnosisResponse ={}", new Object[]{inPatientDiagnosisVo.getInPatientId(),JsonUtils.toJsonString(hisfiaceDiagnosisResponse)});
            if (!"0".equals(hisfiaceDiagnosisResponse.getResultCode())) {
                medicalPatient.setDownLoadDiagnosisExceptionMessage(hisfiaceDiagnosisResponse.getResultMessage());
                medicalPatient.setDownLoadDiagnosisTimes(medicalPatient.getDownLoadDiagnosisTimes() + 1);
                medicalPatientDao.update(medicalPatient);
                responseObject.setResultCodeAndMessage("1001", hisfiaceDiagnosisResponse.getResultMessage());
                return responseObject;
            }

            SBInPatientDiagnosisVoResponse sbInPatientDiagnosisVoResponse = hisfiaceDiagnosisResponse.getResult();

            if (sbInPatientDiagnosisVoResponse != null) {
                MedicalInhospitalDiagnosis medicalInhospitalDiagnosis = buildMedicalInhospitalDiagnosis(sbInPatientDiagnosisVoResponse);
                medicalInhospitalDiagnosis.setTotalRows(2);
                medicalInhospitalDiagnosis.setCurrentOffset(2);
                medicalInhospitalDiagnosis.setOrgCode(medicalPatient.getOrgCode());
                medicalInhospitalDiagnosis.setCaseNo(medicalPatient.getCaseNo());
                medicalInhospitalDiagnosis.setMedicalPatientId(medicalPatient.getId());
                logger.info("inPatientId={},商保住院诊疗信息入库",inPatientDiagnosisVo.getInPatientId());
                medicalInhospitalDiagnosisDao.insert(medicalInhospitalDiagnosis);
                logger.info("inPatientId={},商保住院诊疗信息入库后更新就诊人信息状态",inPatientDiagnosisVo.getInPatientId());
                medicalPatient.setDownLoadDiagnosisTimes(medicalPatient.getDownLoadDiagnosisTimes() + 1);
                medicalPatient.setDownLoadDiagnosisFlag(new Short("1"));
                medicalPatientDao.update(medicalPatient);
            }

        } catch (Exception e) {
            logger.info("获取商保住院诊疗信息系统异常", e);
            responseObject.setResultCodeAndMessage("-1", "获取商保住院诊疗信息系统异常");
            return responseObject;
        }

        return responseObject;
    }


    /**
     * 手术记录已出
     *
     * @param data
     * @return
     */
    public ResponseObject reportNotifySurgery(String data) {
        logger.info("手术记录已经出通知入参data -> {}", data);
        ResponseObject responseObject = new ResponseObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(data, JSONObject.class);
            String hospitalCode = jsonObject.getString("hospital_code");
            String surgeryId = jsonObject.getString("surgery_id");
            String businessType = jsonObject.getString("business_type");
            SBSurgeryRecordVo surgeryRecordVo = new SBSurgeryRecordVo();
            surgeryRecordVo.setSurgeryId(surgeryId);
            surgeryRecordVo.setHospitalCode(hospitalCode);
            surgeryRecordVo.setBusinessType(businessType);
            if ("1".equals(businessType)) {
                return getOutMedicalOperationInfo(surgeryRecordVo);
            } else {
                return getInhospitalOperationInfo(surgeryRecordVo);
            }
        } catch (Exception e) {
            responseObject.setResultCodeAndMessage("-1", "系统异常");
            return responseObject;
        }
    }

    /**
     * 检验检查报告已出
     *
     * @param data
     * @return
     */
    public ResponseObject reportNotifyInspect(String data) {
        logger.info(" 检验检查报告已出通知入参data{}", data);
        ResponseObject responseObject = new ResponseObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(data, JSONObject.class);
            String hospitalCode = jsonObject.getString("hospital_code");
            String reportId = jsonObject.getString("report_id");
            //1：检查报告   2：检验报告
            String reportType = jsonObject.getString("report_type");
            Integer businessType = jsonObject.getInteger("business_type");
            SBInspectRecordVo inspectRecordVo = new SBInspectRecordVo();
            inspectRecordVo.setHospitalCode(hospitalCode);
            inspectRecordVo.setInspectId(reportId);
            if (1 == businessType) {
                if ("2".equals(reportType)) {
                    return getOutMedicalInspectInfo(inspectRecordVo);
                } else {
                    //检查的逻辑
                    return getOutMedicalExaminationInfo(inspectRecordVo);
                }
            } else {
                if ("2".equals(reportType)) {
                    return getInhospitalInspectInfo(inspectRecordVo);
                } else {
                    //检查的逻辑
                    return getInhospitalExaminationInfo(inspectRecordVo);
                }
            }
        } catch (Exception e) {
            responseObject.setResultCodeAndMessage("-1", "失败");
            logger.error("检验检查报告已出通知系统异常", e);
        }
        return responseObject;
    }

    /**
     * 发票补传
     * yyt.sb.notify.invoice.no.report
     *
     * @param data
     * @return
     */
    public ResponseObject reportNotifyInvoiceNo(String data) {
        logger.info("发票编号补传data{}", data);
        ResponseObject responseObject = new ResponseObject();
        try {
            ReportInvoiceNoRequest reportInvoiceNoRequest = JsonUtils.parseObject(data, ReportInvoiceNoRequest.class);
            try {
                Validator.getInstance().validate(reportInvoiceNoRequest);
            } catch (ValidateException ex) {
                logger.info(" 数据校验失败 errMsg={}", ex.getMessage());
                logger.error("error", ex);
                responseObject.setResultCodeAndMessage("1001", "数据校验失败:" + ex.getMessage() + "！");
                return responseObject;
            }
            MedicalOutpatientFee queryMedicalOutpatientFee = new MedicalOutpatientFee();
            queryMedicalOutpatientFee.setPayNo(reportInvoiceNoRequest.getPayFeeId());
            queryMedicalOutpatientFee.setMzFeeId(reportInvoiceNoRequest.getMzFeeId());
            List<MedicalOutpatientFee> medicalOutpatientFeeList = medicalOutpatientFeeDao.select(queryMedicalOutpatientFee);

            if (CollectionUtils.isEmpty(medicalOutpatientFeeList)) {
                responseObject.setResultCodeAndMessage("1002", "系统没有该笔费用");
                return responseObject;
            }
            MedicalInvoiceNo queryMedicalInvoiceNo = new MedicalInvoiceNo();
            queryMedicalInvoiceNo.setMzFeeId(reportInvoiceNoRequest.getMzFeeId());
            queryMedicalInvoiceNo.setPayFeeId(reportInvoiceNoRequest.getPayFeeId());
            List<MedicalInvoiceNo> medicalInvoiceNoList = medicalInvoiceNoDao.select(queryMedicalInvoiceNo);
            if (CollectionUtils.isNotEmpty(medicalInvoiceNoList)) {
                responseObject.setResultCodeAndMessage("0", "发票号已经回调成功");
                return responseObject;
            }

            MedicalInvoiceNo medicalInvoiceNo = new MedicalInvoiceNo();
            medicalInvoiceNo.setId(PKGenerator.generateId());
            medicalInvoiceNo.setMzFeeId(reportInvoiceNoRequest.getMzFeeId());
            medicalInvoiceNo.setPayFeeId(reportInvoiceNoRequest.getPayFeeId());
            medicalInvoiceNo.setOrgCode(reportInvoiceNoRequest.getHospitalCode());
            medicalInvoiceNo.setFinalInvoiceNo(reportInvoiceNoRequest.getFinalInvoiceNo());
            medicalInvoiceNo.setOrgInvoiceNo(reportInvoiceNoRequest.getOrgInvoiceNo());
            medicalInvoiceNo.setBusinessType(reportInvoiceNoRequest.getBusinessType());
            medicalInvoiceNo.setCaseNo(medicalOutpatientFeeList.get(0).getCaseNo());
            medicalInvoiceNo.setMedicalPatientId(medicalOutpatientFeeList.get(0).getMedicalPatientId());
            medicalInvoiceNo.setUploadTimes(0);
            medicalInvoiceNo.setUploadFlag(new Short("0"));
            medicalInvoiceNo.setTotalRows(1);
            medicalInvoiceNo.setCurrentOffset(1);
            medicalInvoiceNo.setCreatedTime(DateUtils.dateToString(new Date()));
            medicalInvoiceNoDao.insert(medicalInvoiceNo);
            responseObject.setResultCodeAndMessage("0", "成功");
        } catch (Exception e) {
            responseObject.setResultCodeAndMessage("-1", "失败");
            logger.error("发票编号补传系统异常", e);
        }
        return responseObject;
    }

    /**
     * 门诊退费通知
     *
     * @param data
     * @return
     */
    public ResponseObject reportNotifyMzRefund(String data) {
        logger.info("收到门诊退费通知data ={}", data);
        ResponseObject responseObject = new ResponseObject();
        ReportSBMZRefundRequest reportSBMZRefundRequest = JsonUtils.parseObject(data, ReportSBMZRefundRequest.class);
        try {

            //1 整单退费
            if ("1".equals(reportSBMZRefundRequest.getRefundType())) {
                MedicalOutpatientFee queryMedicalOutpatientFee = new MedicalOutpatientFee();
                queryMedicalOutpatientFee.setMzFeeId(reportSBMZRefundRequest.getMzFeeId());
                queryMedicalOutpatientFee.setOrgCode(reportSBMZRefundRequest.getHospitalCode());
                List<MedicalOutpatientFee> medicalOutpatientFeeList = medicalOutpatientFeeDao.select(queryMedicalOutpatientFee);
                MedicalOutpatientFeeDetail queryMedicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
                queryMedicalOutpatientFeeDetail.setMzFeeId(reportSBMZRefundRequest.getMzFeeId());
                queryMedicalOutpatientFeeDetail.setOrgCode(reportSBMZRefundRequest.getHospitalCode());
                List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.select(queryMedicalOutpatientFeeDetail);
                if (CollectionUtils.isNotEmpty(medicalOutpatientFeeList)) {
                    for (MedicalOutpatientFee medicalOutpatientFee : medicalOutpatientFeeList) {
                        medicalOutpatientFee.setReFundFeeId(reportSBMZRefundRequest.getRefundFeeId());
                        medicalOutpatientFeeDao.update(medicalOutpatientFee);
                        executeUploadMedicalOutPatientFeeMessage(medicalOutpatientFee);
                    }

                    for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                        executeUploadMedicalOutPatientFeeDetailMessage(medicalOutpatientFeeDetail);
                    }
                }
            } else {
                //部分退费
                MedicalPatient medicalPatient = medicalPatientDao.selectByMzFeeId(reportSBMZRefundRequest.getHospitalCode(), reportSBMZRefundRequest.getMzFeeId());
                if (medicalPatient != null) {
                    getMedicalFeeRecords(medicalPatient);
                }
            }
        } catch (Exception e) {
            logger.error("收到门诊退费通知 data");
        }
        return responseObject;
    }

    public ResponseObject getInhospitalOperationInfo(SBSurgeryRecordVo surgeryRecordVo) {
        ResponseObject responseObject = new ResponseObject();
        //查找对应的住院reportId
        MedicalInhospitalReportids queryMedicalInhospitalReportids = new MedicalInhospitalReportids();
        queryMedicalInhospitalReportids.setOrgCode(surgeryRecordVo.getHospitalCode());
        queryMedicalInhospitalReportids.setReportId(surgeryRecordVo.getSurgeryId());
        queryMedicalInhospitalReportids.setReportType(new Short("3"));
        List<MedicalInhospitalReportids> medicalInhospitalReportidsList = medicalInhospitalReportidsDao.select(queryMedicalInhospitalReportids);
        if (CollectionUtils.isEmpty(medicalInhospitalReportidsList)) {
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;
        }

        MedicalInhospitalReportids medicalInhospitalReportids = medicalInhospitalReportidsList.get(0);
        if (medicalInhospitalReportids.getDownLoadFlag() == 1) {
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;
        }

        logger.info("获取商保住院手术记录his入参 surgeryRecordVo ={}", new Object[]{JsonUtils.toJsonString(surgeryRecordVo)});
        ResCommon<SBSurgeryRecordVoResponse> hisfiaceSBSurgeryRecordVoResponse = HisifaceService.getHisifaceClient().getSBSurgeryRecord(surgeryRecordVo);
        logger.info("获取商保住院手术记录his出参 hisfiaceSBSurgeryRecordVoResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBSurgeryRecordVoResponse)});

        if (!"0".equals(hisfiaceSBSurgeryRecordVoResponse.getResultCode())) {
            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
            responseObject.setResultCodeAndMessage("1001", hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
            return responseObject;
        }

        SBSurgeryRecordVoResponse sbSurgeryRecordVoResponse = hisfiaceSBSurgeryRecordVoResponse.getResult();
        if (sbSurgeryRecordVoResponse == null) {
            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
            responseObject.setResultCodeAndMessage("1001", hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
            return responseObject;
        }

        MedicalOperationRecord medicalOperationRecord = buildMedicalOperationRecord(sbSurgeryRecordVoResponse);
        if (medicalOperationRecord != null) {
            medicalOperationRecord.setMedicalPatientId(medicalInhospitalReportidsList.get(0).getMedicalPatientId());
            medicalOperationRecord.setMedicalOutpatientFeeDetailId(medicalInhospitalReportidsList.get(0).getId());
            medicalOperationRecord.setCaseNo(medicalInhospitalReportidsList.get(0).getCaseNo());
        }

        medicalOperationRecordDao.insert(medicalOperationRecord);
        medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
        medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
        medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
        responseObject.setResultCodeAndMessage("0", "成功");

        return responseObject;
    }

    public ResponseObject getOutMedicalOperationInfo(SBSurgeryRecordVo surgeryRecordVo) {
        ResponseObject responseObject = new ResponseObject();
        try {
            MedicalOperationRecord queryMedicalOperationRecord = new MedicalOperationRecord();
            queryMedicalOperationRecord.setSurgeryId(surgeryRecordVo.getSurgeryId());
            queryMedicalOperationRecord.setOrgCode(surgeryRecordVo.getHospitalCode());
            List<MedicalOperationRecord> dbMedicalOperationRecord = medicalOperationRecordDao.select(queryMedicalOperationRecord);

            if (CollectionUtils.isNotEmpty(dbMedicalOperationRecord)) {
                logger.info("商保门诊手术记录已经获取");
                responseObject.setResultCodeAndMessage("0", "成功");
                return responseObject;
            }

            //查找对应的门诊费用明细
            MedicalOutpatientFeeDetail queryMedicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
            queryMedicalOutpatientFeeDetail.setOrgCode(surgeryRecordVo.getHospitalCode());
            queryMedicalOutpatientFeeDetail.setReportId(surgeryRecordVo.getSurgeryId());
            List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.select(queryMedicalOutpatientFeeDetail);

            logger.info("获取商保门诊手术记录his入参surgeryRecordVo ={}", new Object[]{JsonUtils.toJsonString(surgeryRecordVo)});
            ResCommon<SBSurgeryRecordVoResponse> hisfiaceSBSurgeryRecordVoResponse = HisifaceService.getHisifaceClient().getSBSurgeryRecord(surgeryRecordVo);
            logger.info("获取商保门诊手术记录his入参hisfiaceSBSurgeryRecordVoResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBSurgeryRecordVoResponse)});

            if (!"0".equals(hisfiaceSBSurgeryRecordVoResponse.getResultCode())) {
                responseObject.setResultCodeAndMessage("1001", hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
                return responseObject;
            }
            SBSurgeryRecordVoResponse sbSurgeryRecordVoResponse = hisfiaceSBSurgeryRecordVoResponse.getResult();
            if (sbSurgeryRecordVoResponse == null) {
                responseObject.setResultCodeAndMessage("1001", "未查询到手术报告");
                return responseObject;
            }
            MedicalOperationRecord medicalOperationRecord = buildMedicalOperationRecord(sbSurgeryRecordVoResponse);

            if (CollectionUtils.isNotEmpty(medicalOutpatientFeeDetailList)) {
                //费用已经拉取的话就把检查跟费用明细关联
                if (medicalOperationRecord != null) {
                    for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                        medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                        medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                        medicalOutpatientFeeDetail.setDownLoadReportTimes(medicalOutpatientFeeDetail.getDownLoadReportTimes() + 1);
                        medicalOutpatientFeeDetailDao.update(medicalOutpatientFeeDetail);
                    }
                    medicalOperationRecord.setMedicalPatientId(medicalOutpatientFeeDetailList.get(0).getMedicalPatientId());
                    medicalOperationRecord.setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetailList.get(0).getId());
                    medicalOperationRecord.setCaseNo(medicalOutpatientFeeDetailList.get(0).getCaseNo());
                }
            }

            medicalOperationRecordDao.insert(medicalOperationRecord);
            responseObject.setResultCodeAndMessage("0", "成功");
        } catch (Exception e) {
            logger.info("查询手术记录异常", e);
            responseObject.setResultCodeAndMessage("19999", "查询手术记录异常");
            return responseObject;
        }
        return responseObject;
    }


    public ResponseObject getOutMedicalExaminationInfo(SBInspectRecordVo inspectRecordVo) {
        ResponseObject responseObject = new ResponseObject();
        try {
            MedicalExamination queryMedicalExamination = new MedicalExamination();
            queryMedicalExamination.setOrgCode(inspectRecordVo.getHospitalCode());
            queryMedicalExamination.setCheckId(inspectRecordVo.getInspectId());
            List<MedicalExamination> dbMedicalExamination = medicalExaminationDao.select(queryMedicalExamination);
            if (CollectionUtils.isNotEmpty(dbMedicalExamination)) {
                responseObject.setResultCodeAndMessage("0", "成功");
                return responseObject;
            }
            //查找对应的门诊费用明细
            MedicalOutpatientFeeDetail queryMedicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
            queryMedicalOutpatientFeeDetail.setOrgCode(inspectRecordVo.getHospitalCode());
            queryMedicalOutpatientFeeDetail.setReportId(inspectRecordVo.getInspectId());
            List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.select(queryMedicalOutpatientFeeDetail);

            //拿检查
            SBCheckRecordVo checkRecordVo = new SBCheckRecordVo();
            checkRecordVo.setHospitalCode(inspectRecordVo.getHospitalCode());
            checkRecordVo.setCheckId(inspectRecordVo.getInspectId());
            logger.info("获取门诊商保检查信息his入参checkRecordVo ={}", new Object[]{JsonUtils.toJsonString(checkRecordVo)});
            ResCommon<SBCheckRecordVoResponse> hisfiaceSBCheckRecordResponse = HisifaceService.getHisifaceClient().getSBCheckRecord(checkRecordVo);
            logger.info("获取门诊商保检查信息his出参 hisfiaceSBCheckRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBCheckRecordResponse)});

            if (!"0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
                responseObject.setResultCodeAndMessage("1001", hisfiaceSBCheckRecordResponse.getResultMessage());
                return responseObject;
            }
            SBCheckRecordVoResponse sbCheckRecordVoResponse = hisfiaceSBCheckRecordResponse.getResult();
            if (sbCheckRecordVoResponse == null) {
                responseObject.setResultCodeAndMessage("1001", "获取商保检查信息失败");
                return responseObject;
            }

            MedicalExamination examination = buildExamination(sbCheckRecordVoResponse);
            examination.setOrgCode(inspectRecordVo.getHospitalCode());
            if (CollectionUtils.isNotEmpty(medicalOutpatientFeeDetailList)) {
                //费用已经拉取的话就把检查跟费用明细关联
                if (examination != null) {
                    for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                        medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                        medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                        medicalOutpatientFeeDetail.setDownLoadReportTimes(medicalOutpatientFeeDetail.getDownLoadReportTimes() + 1);
                        medicalOutpatientFeeDetailDao.update(medicalOutpatientFeeDetail);
                    }
                    examination.setMedicalPatientId(medicalOutpatientFeeDetailList.get(0).getMedicalPatientId());
                    examination.setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetailList.get(0).getId());
                    examination.setCaseNo(medicalOutpatientFeeDetailList.get(0).getCaseNo());
                }
            }
            medicalExaminationDao.insert(examination);
            responseObject.setResultCodeAndMessage("0", "成功");
        } catch (Exception e) {
            logger.info("获取门诊商保检查信息系统异常", e);
            responseObject.setResultCodeAndMessage("-1", "获取门诊商保检查信息系统异常");
        }
        return responseObject;
    }

    public ResponseObject getInhospitalExaminationInfo(SBInspectRecordVo inspectRecordVo) {
        ResponseObject responseObject = new ResponseObject();
        MedicalInhospitalReportids queryMedicalInhospitalReportids = new MedicalInhospitalReportids();
        queryMedicalInhospitalReportids.setOrgCode(inspectRecordVo.getHospitalCode());
        queryMedicalInhospitalReportids.setReportId(inspectRecordVo.getInspectId());
        List<MedicalInhospitalReportids> medicalInhospitalReportidsList = medicalInhospitalReportidsDao.select(queryMedicalInhospitalReportids);

        if (CollectionUtils.isEmpty(medicalInhospitalReportidsList)) {
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;
        }

        MedicalInhospitalReportids medicalInhospitalReportids = medicalInhospitalReportidsList.get(0);
        if (medicalInhospitalReportids.getDownLoadFlag() == 1) {
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;
        }

        //拿检查
        SBCheckRecordVo checkRecordVo = new SBCheckRecordVo();
        checkRecordVo.setHospitalCode(inspectRecordVo.getHospitalCode());
        checkRecordVo.setCheckId(inspectRecordVo.getInspectId());
        logger.info("获取住院商保检查信息his入参 checkRecordVo ={}", new Object[]{JsonUtils.toJsonString(checkRecordVo)});
        ResCommon<SBCheckRecordVoResponse> hisfiaceSBCheckRecordResponse = HisifaceService.getHisifaceClient().getSBCheckRecord(checkRecordVo);
        logger.info("获取住院商保检查信息his出参 hisfiaceSBCheckRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBCheckRecordResponse)});

        if (!"0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBCheckRecordResponse.getResultMessage());
            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
            responseObject.setResultCodeAndMessage("1001", hisfiaceSBCheckRecordResponse.getResultMessage());
            return responseObject;
        }
        SBCheckRecordVoResponse sbCheckRecordVoResponse = hisfiaceSBCheckRecordResponse.getResult();
        if (sbCheckRecordVoResponse == null) {
            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
            medicalInhospitalReportids.setExceptionMessage(hisfiaceSBCheckRecordResponse.getResultMessage());
            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
            responseObject.setResultCodeAndMessage("1001", hisfiaceSBCheckRecordResponse.getResultMessage());
            return responseObject;
        }

        MedicalExamination examination = buildExamination(sbCheckRecordVoResponse);
        examination.setOrgCode(inspectRecordVo.getHospitalCode());
        if (CollectionUtils.isNotEmpty(medicalInhospitalReportidsList)) {
            //费用已经拉取的话就把检查跟费用明细关联
            if (examination != null) {
                examination.setMedicalPatientId(medicalInhospitalReportidsList.get(0).getMedicalPatientId());
                examination.setMedicalOutpatientFeeDetailId(medicalInhospitalReportidsList.get(0).getId());
                examination.setCaseNo(medicalInhospitalReportidsList.get(0).getCaseNo());
            }
        }
        medicalExaminationDao.insert(examination);
        medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
        medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
        medicalInhospitalReportidsDao.update(medicalInhospitalReportids);

        responseObject.setResultCodeAndMessage("0", "成功");
        return responseObject;
    }

    public ResponseObject getInhospitalInspectInfo(SBInspectRecordVo inspectRecordVo) {
        ResponseObject responseObject = new ResponseObject();
        MedicalInhospitalReportids queryMedicalInhospitalReportids = new MedicalInhospitalReportids();
        queryMedicalInhospitalReportids.setOrgCode(inspectRecordVo.getHospitalCode());
        queryMedicalInhospitalReportids.setReportId(inspectRecordVo.getInspectId());
        queryMedicalInhospitalReportids.setReportType(new Short("2"));
        List<MedicalInhospitalReportids> medicalInhospitalReportids = medicalInhospitalReportidsDao.select(queryMedicalInhospitalReportids);
        if (CollectionUtils.isEmpty(medicalInhospitalReportids)) {
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;
        }

        MedicalInhospitalReportids medicalInhospitalReportid = medicalInhospitalReportids.get(0);
        if (1 == medicalInhospitalReportid.getDownLoadFlag()) {
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;
        }

        //这个reportId 如果没有拉取过检验才去拉取
        MedicalChecking medicalChecking = null;
        List<MedicalCheckingDetail> medicalCheckingDetails = null;
        //拿到检验
        logger.info("获取商保检验信息his入参 inspectRecordVo ={}", new Object[]{JsonUtils.toJsonString(inspectRecordVo)});
        ResCommon<SBInspectRecordVoResponse> hisfiaceSBInspectRecordResponse = HisifaceService.getHisifaceClient().getSBInspectRecord(inspectRecordVo);
        logger.info("获取商保检验信息his出参 hisfiaceSBInspectRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBInspectRecordResponse)});

        if (!"0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
            medicalInhospitalReportid.setExceptionMessage(hisfiaceSBInspectRecordResponse.getResultMessage());
            medicalInhospitalReportid.setDownLoadTimes(medicalInhospitalReportid.getDownLoadTimes() + 1);
            medicalInhospitalReportidsDao.update(medicalInhospitalReportid);
            responseObject.setResultCodeAndMessage("1001", hisfiaceSBInspectRecordResponse.getResultMessage());
            return responseObject;
        }
        SBInspectRecordVoResponse sBInspectRecordVoResponse = hisfiaceSBInspectRecordResponse.getResult();
        if (sBInspectRecordVoResponse == null) {
            medicalInhospitalReportid.setExceptionMessage(hisfiaceSBInspectRecordResponse.getResultMessage());
            medicalInhospitalReportid.setDownLoadTimes(medicalInhospitalReportid.getDownLoadTimes() + 1);
            medicalInhospitalReportidsDao.update(medicalInhospitalReportid);
            responseObject.setResultCodeAndMessage("1001", hisfiaceSBInspectRecordResponse.getResultMessage());
            return responseObject;
        }

        medicalChecking = buildMedicalChecking(sBInspectRecordVoResponse);
        //患者信息id
        List<SBInspectDetail> inspectDetails = sBInspectRecordVoResponse.getInspectDetails();
        if (CollectionUtils.isNotEmpty(inspectDetails)) {
            medicalCheckingDetails = new ArrayList<>();
            for (SBInspectDetail sBInspectDetail : inspectDetails) {
                MedicalCheckingDetail medicalCheckingDetail = buildMedicalCheckingDetail(sBInspectDetail);
                medicalCheckingDetail.setBusinessFeeId(medicalChecking.getBusinessFeeId());
                medicalCheckingDetail.setInspectId(medicalChecking.getInspectId());
                medicalCheckingDetail.setBusinessType(converterStringToShort(sBInspectRecordVoResponse.getBusinessType()));
                medicalCheckingDetail.setOrgCode(medicalChecking.getOrgCode());
                medicalCheckingDetail.setMedicalCheckingId(medicalChecking.getId());
                medicalCheckingDetails.add(medicalCheckingDetail);
            }
        }

        //费用已经拉取的话就把检验跟费用明细关联
        if (medicalChecking != null && CollectionUtils.isNotEmpty(medicalCheckingDetails)) {
            medicalChecking.setMedicalPatientId(medicalInhospitalReportids.get(0).getMedicalPatientId());
            medicalChecking.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.get(0).getId());
            medicalChecking.setCaseNo(medicalInhospitalReportids.get(0).getCaseNo());
            for (MedicalCheckingDetail medicalCheckingDetail : medicalCheckingDetails) {
                medicalCheckingDetail.setMedicalPatientId(medicalInhospitalReportid.getMedicalPatientId());
                medicalCheckingDetail.setCaseNo(medicalInhospitalReportid.getCaseNo());
            }
        }

        medicalCheckingDao.insert(medicalChecking);
        if (CollectionUtils.isNotEmpty(medicalCheckingDetails)) {
            medicalCheckingDetailDao.batchInsert(medicalCheckingDetails);
        }
        medicalInhospitalReportid.setDownLoadFlag(new Short("1"));
        medicalInhospitalReportid.setDownLoadTimes(1);
        medicalInhospitalReportidsDao.update(medicalInhospitalReportid);
        responseObject.setResultCodeAndMessage("0", "成功");
        return responseObject;
    }


    public ResponseObject getOutMedicalInspectInfo(SBInspectRecordVo inspectRecordVo) {
        ResponseObject responseObject = new ResponseObject();
        try {
            MedicalChecking queryMedicalChecking = new MedicalChecking();
            queryMedicalChecking.setOrgCode(inspectRecordVo.getHospitalCode());
            queryMedicalChecking.setInspectId(inspectRecordVo.getInspectId());
            List<MedicalChecking> dbMedicalChecking = medicalCheckingDao.select(queryMedicalChecking);
            if (CollectionUtils.isNotEmpty(dbMedicalChecking)) {
                responseObject.setResultCodeAndMessage("0", "成功");
                return responseObject;
            }

            //查找对应的门诊费用明细
            MedicalOutpatientFeeDetail queryMedicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
            queryMedicalOutpatientFeeDetail.setOrgCode(inspectRecordVo.getHospitalCode());
            queryMedicalOutpatientFeeDetail.setReportId(inspectRecordVo.getInspectId());
            List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = medicalOutpatientFeeDetailDao.select(queryMedicalOutpatientFeeDetail);
            //通过reportId 查找对应的检验
            MedicalChecking medicalChecking = null;
            List<MedicalCheckingDetail> medicalCheckingDetails = null;

            //拿到检验
            logger.info("获取商保检验信息his入参 inspectRecordVo ={}", new Object[]{JsonUtils.toJsonString(inspectRecordVo)});
            ResCommon<SBInspectRecordVoResponse> hisfiaceSBInspectRecordResponse = HisifaceService.getHisifaceClient().getSBInspectRecord(inspectRecordVo);
            logger.info("获取商保检验信息his出参 hisfiaceSBInspectRecordResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceSBInspectRecordResponse)});
            if (!"0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
                responseObject.setResultCodeAndMessage("1001", hisfiaceSBInspectRecordResponse.getResultMessage());
                return responseObject;
            }

            SBInspectRecordVoResponse sBInspectRecordVoResponse = hisfiaceSBInspectRecordResponse.getResult();
            if (sBInspectRecordVoResponse == null) {
                responseObject.setResultCodeAndMessage("1001", "获取商保检验信息失败");
                return responseObject;
            }

            medicalChecking = buildMedicalChecking(sBInspectRecordVoResponse);
            //患者信息id
            List<SBInspectDetail> inspectDetails = sBInspectRecordVoResponse.getInspectDetails();
            if (CollectionUtils.isNotEmpty(inspectDetails)) {
                medicalCheckingDetails = new ArrayList<MedicalCheckingDetail>();
                for (SBInspectDetail sBInspectDetail : inspectDetails) {
                    MedicalCheckingDetail medicalCheckingDetail = buildMedicalCheckingDetail(sBInspectDetail);
                    medicalCheckingDetail.setBusinessFeeId(medicalChecking.getBusinessFeeId());
                    medicalCheckingDetail.setInspectId(medicalChecking.getInspectId());
                    medicalCheckingDetail.setBusinessType(converterStringToShort(sBInspectRecordVoResponse.getBusinessType()));
                    medicalCheckingDetail.setOrgCode(medicalChecking.getOrgCode());
                    medicalCheckingDetail.setMedicalCheckingId(medicalChecking.getId());
                    medicalCheckingDetails.add(medicalCheckingDetail);
                }
            }

            if (CollectionUtils.isNotEmpty(medicalOutpatientFeeDetailList)) {
                //费用已经拉取的话就把检验跟费用明细关联
                if (medicalChecking != null && CollectionUtils.isNotEmpty(medicalCheckingDetails)) {
                    for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                        medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("1"));
                        medicalOutpatientFeeDetail.setNotifyFlag(new Short("1"));
                        medicalOutpatientFeeDetail.setDownLoadReportTimes(medicalOutpatientFeeDetail.getDownLoadReportTimes() + 1);
                        medicalOutpatientFeeDetailDao.update(medicalOutpatientFeeDetail);
                    }

                    medicalChecking.setMedicalPatientId(medicalOutpatientFeeDetailList.get(0).getMedicalPatientId());
                    medicalChecking.setMedicalOutpatientFeeDetailId(medicalOutpatientFeeDetailList.get(0).getId());
                    medicalChecking.setCaseNo(medicalOutpatientFeeDetailList.get(0).getCaseNo());
                    for (MedicalCheckingDetail medicalCheckingDetail : medicalCheckingDetails) {
                        medicalCheckingDetail.setMedicalPatientId(medicalOutpatientFeeDetailList.get(0).getMedicalPatientId());
                        medicalCheckingDetail.setCaseNo(medicalOutpatientFeeDetailList.get(0).getCaseNo());
                    }
                }
            }

            medicalCheckingDao.insert(medicalChecking);
            medicalCheckingDetailDao.batchInsert(medicalCheckingDetails);
            responseObject.setResultCodeAndMessage("0", "成功");
            return responseObject;

        } catch (Exception e) {
            logger.info("获取商保检验信息异常", e);
            responseObject.setResultCodeAndMessage("-1", "获取商保检验信息异常");
            return responseObject;
        }
    }

    private MedicalCheckingDetail buildMedicalCheckingDetail(SBInspectDetail sBInspectDetail) {
        MedicalCheckingDetail medicalCheckingDetail = new MedicalCheckingDetail();
        medicalCheckingDetail.setId(PKGenerator.generateId());
        medicalCheckingDetail.setItemCode(sBInspectDetail.getItemCode());
        medicalCheckingDetail.setItemName(sBInspectDetail.getItemName());
        medicalCheckingDetail.setInspectMethod(sBInspectDetail.getInspectMethod());
        medicalCheckingDetail.setInspectDesc(sBInspectDetail.getInspectDesc());
        medicalCheckingDetail.setSampleType(sBInspectDetail.getSampleType());
        medicalCheckingDetail.setSampleCollectPart(sBInspectDetail.getSampleCollectPart());
        medicalCheckingDetail.setInspectResult(sBInspectDetail.getInspectResult());
        medicalCheckingDetail.setInspectDetailId(sBInspectDetail.getInspectDetailId());
        medicalCheckingDetail.setAuditTime(sBInspectDetail.getAuditTime());
        medicalCheckingDetail.setReportDoctorCode(sBInspectDetail.getReportDoctorCode());
        medicalCheckingDetail.setReportDoctorName(sBInspectDetail.getReportDoctorName());
        medicalCheckingDetail.setReportTime(sBInspectDetail.getReportTime());
        medicalCheckingDetail.setInspectResultCode(sBInspectDetail.getInspectResultCode());
        medicalCheckingDetail.setResultValue(sBInspectDetail.getResultValue());
        medicalCheckingDetail.setUnit(sBInspectDetail.getUnit());
        medicalCheckingDetail.setRefRange(sBInspectDetail.getRefRange());
        medicalCheckingDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        medicalCheckingDetail.setOrgCode(HOSPITAL_CODE);
        medicalCheckingDetail.setUploadFlag(new Short("0"));
        medicalCheckingDetail.setUploadTimes(0);
        return medicalCheckingDetail;
    }

    private MedicalChecking buildMedicalChecking(SBInspectRecordVoResponse sBInspectRecordVoResponse) {
        MedicalChecking medicalChecking = new MedicalChecking();
        medicalChecking.setId(PKGenerator.generateId());
        medicalChecking.setBusinessFeeId(sBInspectRecordVoResponse.getBusinessFeeId());
        medicalChecking.setBusinessType(converterStringToShort(sBInspectRecordVoResponse.getBusinessType()));
        medicalChecking.setInspectId(sBInspectRecordVoResponse.getInspectId());
        medicalChecking.setMzBillId(sBInspectRecordVoResponse.getMzBillId());
        medicalChecking.setAdmissionNo(sBInspectRecordVoResponse.getAdmissionNo());
        medicalChecking.setPatId(sBInspectRecordVoResponse.getPatId());
        medicalChecking.setSendHospitalCode(sBInspectRecordVoResponse.getSendHospitalCode());
        medicalChecking.setInspectTime(sBInspectRecordVoResponse.getInspectTime());
        medicalChecking.setSendDeptCode(sBInspectRecordVoResponse.getSendDeptCode());
        medicalChecking.setSendDeptName(sBInspectRecordVoResponse.getSendDeptName());
        medicalChecking.setSendDoctorCode(sBInspectRecordVoResponse.getSendDoctorCode());
        medicalChecking.setSendDoctorName(sBInspectRecordVoResponse.getSendDoctorName());
        medicalChecking.setInspectType(converterStringToShort(sBInspectRecordVoResponse.getInspectType()));
        medicalChecking.setInspectDeptCode(sBInspectRecordVoResponse.getInspectDeptCode());
        medicalChecking.setInspectDeptName(sBInspectRecordVoResponse.getSendDeptName());
        medicalChecking.setExecDoctorCode(sBInspectRecordVoResponse.getExecDoctorCode());
        medicalChecking.setExecDoctorName(sBInspectRecordVoResponse.getExecDoctorName());
        medicalChecking.setAuditDoctorCode(sBInspectRecordVoResponse.getAuditDoctorCode());
        medicalChecking.setAuditDoctorName(sBInspectRecordVoResponse.getAuditDoctorName());
        medicalChecking.setResultDesc(sBInspectRecordVoResponse.getResultDesc());
        medicalChecking.setCreatedTime(DateUtils.dateToString(new Date()));
        medicalChecking.setOrgCode(HOSPITAL_CODE);
        medicalChecking.setUploadFlag(new Short("0"));
        medicalChecking.setUploadTimes(0);
        return medicalChecking;
    }


    private MedicalExamination buildExamination(SBCheckRecordVoResponse sbCheckRecordVoResponse) {
        MedicalExamination medicalExamination = new MedicalExamination();
        medicalExamination.setId(PKGenerator.generateId());
        medicalExamination.setBusinessFeeId(sbCheckRecordVoResponse.getBusinessFeeId());
        medicalExamination.setBusinessType(converterStringToShort(sbCheckRecordVoResponse.getBusinessType()));
        medicalExamination.setCheckId(sbCheckRecordVoResponse.getCheckId());
        medicalExamination.setMzBillId(sbCheckRecordVoResponse.getMzBillId());
        medicalExamination.setAdmissionNo(sbCheckRecordVoResponse.getAdmissionNo());
        medicalExamination.setPatId(sbCheckRecordVoResponse.getPatId());
        medicalExamination.setSendDeptName(sbCheckRecordVoResponse.getSendDeptName());
        medicalExamination.setSendDoctorName(sbCheckRecordVoResponse.getSendDoctorName());
        medicalExamination.setSendDoctorName(sbCheckRecordVoResponse.getSendDoctorName());
        medicalExamination.setEmergencyCheckFlag(converterStringToShort(sbCheckRecordVoResponse.getEmergencyCheckFlag()));
        medicalExamination.setCheckDeptCode(sbCheckRecordVoResponse.getCheckDeptCode());
        medicalExamination.setCheckDeptName(sbCheckRecordVoResponse.getCheckDeptName());
        medicalExamination.setExecDoctorCode(sbCheckRecordVoResponse.getExecDoctorCode());
        medicalExamination.setExecDoctorName(sbCheckRecordVoResponse.getExecDoctorName());
        medicalExamination.setAuditDoctorName(sbCheckRecordVoResponse.getAuditDoctorName());
        medicalExamination.setAuditTime(sbCheckRecordVoResponse.getAuditTime());
        medicalExamination.setCheckType(sbCheckRecordVoResponse.getCheckType());
        medicalExamination.setCheckTime(sbCheckRecordVoResponse.getCheckTime());
        medicalExamination.setCheckMethod(sbCheckRecordVoResponse.getCheckMethod());
        medicalExamination.setCheckDesc(sbCheckRecordVoResponse.getCheckDesc());
        medicalExamination.setItemCode(sbCheckRecordVoResponse.getItemCode());
        medicalExamination.setChineseName(sbCheckRecordVoResponse.getChineseName());
        medicalExamination.setEnglishName(sbCheckRecordVoResponse.getEnglishName());
        medicalExamination.setCheckResultCode(sbCheckRecordVoResponse.getCheckResultCode());
        medicalExamination.setCheckResultDesc(sbCheckRecordVoResponse.getCheckResultDesc());
        medicalExamination.setImagingDesc(sbCheckRecordVoResponse.getImagingDesc());
        medicalExamination.setImagingDiagnosis(sbCheckRecordVoResponse.getImagingDiagnosis());
        medicalExamination.setReportDoctorCode(sbCheckRecordVoResponse.getReportDoctorCode());
        medicalExamination.setReportDoctorName(sbCheckRecordVoResponse.getReportDoctorName());
        medicalExamination.setReportTime(sbCheckRecordVoResponse.getReportTime());
        medicalExamination.setRisrelationNo(sbCheckRecordVoResponse.getRisRelationNo());
        medicalExamination.setImagingId(sbCheckRecordVoResponse.getImagingId());
        medicalExamination.setDeviceType(sbCheckRecordVoResponse.getDeviceType());
        medicalExamination.setClinicalDiagnosis(sbCheckRecordVoResponse.getClinicalDiagnosis());
        medicalExamination.setCheckPartCode(sbCheckRecordVoResponse.getCheckPartCode());
        medicalExamination.setCheckPart(sbCheckRecordVoResponse.getCheckPart());
        medicalExamination.setPictureNo(sbCheckRecordVoResponse.getPictureNo());
        medicalExamination.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalExamination;
    }

    private MedicalOperationRecord buildMedicalOperationRecord(SBSurgeryRecordVoResponse sbSurgeryRecordVoResponse) {
        MedicalOperationRecord medicalOperationRecord = new MedicalOperationRecord();
        medicalOperationRecord.setId(PKGenerator.generateId());
        medicalOperationRecord.setInpatientId(sbSurgeryRecordVoResponse.getInPatientId());
        medicalOperationRecord.setBusinessType(converterStringToShort(sbSurgeryRecordVoResponse.getBusinessType()));
        medicalOperationRecord.setPatId(sbSurgeryRecordVoResponse.getPatId());
        medicalOperationRecord.setAdmissionNo(sbSurgeryRecordVoResponse.getAdmissionNo());
        medicalOperationRecord.setInTime(conveterStringToInt(sbSurgeryRecordVoResponse.getInTime()));
        medicalOperationRecord.setMzFeeId(sbSurgeryRecordVoResponse.getMzFeeId());
        medicalOperationRecord.setSurgeryId(sbSurgeryRecordVoResponse.getSurgeryId());
        medicalOperationRecord.setSurgeryTime(sbSurgeryRecordVoResponse.getSurgeryTime());
        medicalOperationRecord.setPreDiagnosis(sbSurgeryRecordVoResponse.getPreDiagnosis());
        medicalOperationRecord.setSurgeryCode(sbSurgeryRecordVoResponse.getSurgeryCode());
        medicalOperationRecord.setSurgeryName(sbSurgeryRecordVoResponse.getSurgeryName());
        medicalOperationRecord.setSurgeryDoctor(sbSurgeryRecordVoResponse.getSurgeryDoctor());
        medicalOperationRecord.setAssistantName(sbSurgeryRecordVoResponse.getAssistantName());
        medicalOperationRecord.setAnaesthesiaMethod(sbSurgeryRecordVoResponse.getAnaesthesiaMethod());
        medicalOperationRecord.setProSurgeryManage(sbSurgeryRecordVoResponse.getProSurgeryManage());
        medicalOperationRecord.setElectronicMedicalRecords(sbSurgeryRecordVoResponse.getElectronicMedicalRecords());
        medicalOperationRecord.setItemSpec(sbSurgeryRecordVoResponse.getItemSpec());
        medicalOperationRecord.setItemType(sbSurgeryRecordVoResponse.getItemType());
        medicalOperationRecord.setItemUnit(sbSurgeryRecordVoResponse.getItemUnit());
        medicalOperationRecord.setItemOrigin(sbSurgeryRecordVoResponse.getItemOrigin());
        medicalOperationRecord.setPackageQty(converterStringToInt(sbSurgeryRecordVoResponse.getPackageQty()));
        medicalOperationRecord.setPackageUnit(sbSurgeryRecordVoResponse.getPackageUnit());
        medicalOperationRecord.setMiniDosageUnit(sbSurgeryRecordVoResponse.getMiniDosageUnit());
        medicalOperationRecord.setPerDose(sbSurgeryRecordVoResponse.getPerDose());
        medicalOperationRecord.setDosageUnit(sbSurgeryRecordVoResponse.getDosageUnit());
        medicalOperationRecord.setPerDayTimes(converterStringToInt(sbSurgeryRecordVoResponse.getPerDayTimes()));
        medicalOperationRecord.setMedicinedays(converterStringToInt(sbSurgeryRecordVoResponse.getMedicineDays()));
        medicalOperationRecord.setSingleCompoundFlag(sbSurgeryRecordVoResponse.getSingleCompoundFlag());
        medicalOperationRecord.setChineseHerbalMedicineQty(converterStringToShort(sbSurgeryRecordVoResponse.getChineseHerbalMedicineQty()));
        medicalOperationRecord.setItemLimitPrice(converterStringToInt(sbSurgeryRecordVoResponse.getChineseHerbalMedicineQty()));
        medicalOperationRecord.setApprovalNo(sbSurgeryRecordVoResponse.getApprovalNo());
        medicalOperationRecord.setSelfFeeFlag(sbSurgeryRecordVoResponse.getSelfFeeFlag());
        medicalOperationRecord.setSpecialMedicineFlag(sbSurgeryRecordVoResponse.getSpecialMedicineFlag());
        medicalOperationRecord.setInsuranceAuxiliaryInfo(sbSurgeryRecordVoResponse.getInsuranceAuxiliaryInfo());
        medicalOperationRecord.setSingleQty(converterStringToShort(sbSurgeryRecordVoResponse.getSingleQty()));
        medicalOperationRecord.setFrequencyValue(converterStringToInt(sbSurgeryRecordVoResponse.getFrequencyValue()));
        medicalOperationRecord.setMedicineMethod(sbSurgeryRecordVoResponse.getMedicineMethod());
        medicalOperationRecord.setFrequencyCode(sbSurgeryRecordVoResponse.getFrequencyCode());
        medicalOperationRecord.setItemCode(sbSurgeryRecordVoResponse.getItemCode());
        medicalOperationRecord.setDrugSn(sbSurgeryRecordVoResponse.getDrugSn());
        medicalOperationRecord.setDrugOrigin(sbSurgeryRecordVoResponse.getDrugOrigin());
        medicalOperationRecord.setDrugSpecOrigin(converterStringToInt(sbSurgeryRecordVoResponse.getDrugSpecOrigin()));
        medicalOperationRecord.setItemExecDate(sbSurgeryRecordVoResponse.getItemExecDate());
        medicalOperationRecord.setDoctorCode(sbSurgeryRecordVoResponse.getDoctorCode());
        medicalOperationRecord.setDoctorName(sbSurgeryRecordVoResponse.getDoctorName());
        medicalOperationRecord.setCombination(sbSurgeryRecordVoResponse.getCombination());
        medicalOperationRecord.setFunctions(sbSurgeryRecordVoResponse.getFunctions());
        medicalOperationRecord.setContraindication(sbSurgeryRecordVoResponse.getContraindication());
        medicalOperationRecord.setRecipeFlag(sbSurgeryRecordVoResponse.getRecipeFlag());
        medicalOperationRecord.setRestrictionsFlag(sbSurgeryRecordVoResponse.getRestrictionsFlag());
        medicalOperationRecord.setRestrictions(sbSurgeryRecordVoResponse.getRestrictions());
        medicalOperationRecord.setBaseDrugIdentification(sbSurgeryRecordVoResponse.getBaseDrugIdentification());
        medicalOperationRecord.setNationalMedicineId(sbSurgeryRecordVoResponse.getNationalMedicineId());
        medicalOperationRecord.setRegId(sbSurgeryRecordVoResponse.getRegId());
        medicalOperationRecord.setBrand(sbSurgeryRecordVoResponse.getBrand());
        medicalOperationRecord.setItemEnglishName(sbSurgeryRecordVoResponse.getItemEnglishName());
        medicalOperationRecord.setInsuranceRange(sbSurgeryRecordVoResponse.getInsuranceRange());
        medicalOperationRecord.setMaxLimitPrice1(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice1()));
        medicalOperationRecord.setMaxLimitPrice2(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice2()));
        medicalOperationRecord.setMaxLimitPrice3(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice3()));
        medicalOperationRecord.setMaxLimitPrice4(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice4()));
        medicalOperationRecord.setInsuranceSettleRangeAmount(converterStringToInt(sbSurgeryRecordVoResponse.getInsuranceSettleRangeAmount()));
        medicalOperationRecord.setManufacturer(sbSurgeryRecordVoResponse.getManufacturer());
        medicalOperationRecord.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalOperationRecord;
    }


    private MedicalInhospitalDiagnosis buildMedicalInhospitalDiagnosis(SBInPatientDiagnosisVoResponse sbInPatientDiagnosisVoResponse) {
        MedicalInhospitalDiagnosis medicalInhospitalDiagnosis = new MedicalInhospitalDiagnosis();
        medicalInhospitalDiagnosis.setId(PKGenerator.generateId());
        medicalInhospitalDiagnosis.setInDeptName(sbInPatientDiagnosisVoResponse.getInDeptName());
        medicalInhospitalDiagnosis.setInpatientId(sbInPatientDiagnosisVoResponse.getInPatientId());
        medicalInhospitalDiagnosis.setPatId(sbInPatientDiagnosisVoResponse.getPatId());
        medicalInhospitalDiagnosis.setAdmissionNo(sbInPatientDiagnosisVoResponse.getAdmissionNo());
        medicalInhospitalDiagnosis.setInTime(conveterStringToInt(sbInPatientDiagnosisVoResponse.getInTime()));
        medicalInhospitalDiagnosis.setHospitalCode(sbInPatientDiagnosisVoResponse.getHospitalCode());
        medicalInhospitalDiagnosis.setHospitalName(sbInPatientDiagnosisVoResponse.getHospitalName());
        medicalInhospitalDiagnosis.setHospitalType(conveterStringToShort(sbInPatientDiagnosisVoResponse.getHospitalType()));
        medicalInhospitalDiagnosis.setMedicalType(conveterStringToShort(sbInPatientDiagnosisVoResponse.getMedicalType()));
        medicalInhospitalDiagnosis.setHouseDoctorCode(sbInPatientDiagnosisVoResponse.getHouseDoctorCode());
        medicalInhospitalDiagnosis.setHouseDoctorName(sbInPatientDiagnosisVoResponse.getHouseDoctorName());
        medicalInhospitalDiagnosis.setAttendingDoctorCode(sbInPatientDiagnosisVoResponse.getAttendingDoctorCode());
        medicalInhospitalDiagnosis.setAttendingDoctorName(sbInPatientDiagnosisVoResponse.getAttendingDoctorName());
        medicalInhospitalDiagnosis.setChiefDoctorCode(sbInPatientDiagnosisVoResponse.getChiefDoctorCode());
        medicalInhospitalDiagnosis.setChiefDoctorName(sbInPatientDiagnosisVoResponse.getChiefDoctorName());
        medicalInhospitalDiagnosis.setInPatientTime(sbInPatientDiagnosisVoResponse.getInPatientTime());
        medicalInhospitalDiagnosis.setInDeptTime(sbInPatientDiagnosisVoResponse.getInDeptTime());
        medicalInhospitalDiagnosis.setInMethod(sbInPatientDiagnosisVoResponse.getInMethod());
        medicalInhospitalDiagnosis.setInReason(sbInPatientDiagnosisVoResponse.getInReason());
        medicalInhospitalDiagnosis.setEndemicArea(sbInPatientDiagnosisVoResponse.getEndemicArea());
        medicalInhospitalDiagnosis.setInDeptCode(sbInPatientDiagnosisVoResponse.getInDeptCode());
        medicalInhospitalDiagnosis.setBedNo(sbInPatientDiagnosisVoResponse.getBedNo());
        medicalInhospitalDiagnosis.setOutDeptName(sbInPatientDiagnosisVoResponse.getOutDeptName());
        medicalInhospitalDiagnosis.setOutDeptCode(sbInPatientDiagnosisVoResponse.getOutDeptCode());
        medicalInhospitalDiagnosis.setOutPatientTime(trimValue(sbInPatientDiagnosisVoResponse.getOutPatientTime()));
        medicalInhospitalDiagnosis.setDeadTime(sbInPatientDiagnosisVoResponse.getDeadTime());
        medicalInhospitalDiagnosis.setDeadReason(sbInPatientDiagnosisVoResponse.getDeadReason());
        medicalInhospitalDiagnosis.setInDiagnosisCode(sbInPatientDiagnosisVoResponse.getInDiagnosisCode());
        medicalInhospitalDiagnosis.setInDiagnosisName(sbInPatientDiagnosisVoResponse.getInDiagnosisName());
        medicalInhospitalDiagnosis.setInDiagnosisDoctorCode(sbInPatientDiagnosisVoResponse.getInDiagnosisDoctorCode());
        medicalInhospitalDiagnosis.setInMedicalDiagnosisName(sbInPatientDiagnosisVoResponse.getInMedicalDiagnosisName());
        medicalInhospitalDiagnosis.setMedicalHistoryStatement(sbInPatientDiagnosisVoResponse.getMedicalHistoryStatement());
        medicalInhospitalDiagnosis.setHistoryPresentIllness(sbInPatientDiagnosisVoResponse.getHistoryPresentIllness());
        medicalInhospitalDiagnosis.setPastHistory(sbInPatientDiagnosisVoResponse.getPastHistory());
        medicalInhospitalDiagnosis.setMaritalHistory(sbInPatientDiagnosisVoResponse.getMaritalHistory());
        medicalInhospitalDiagnosis.setPersonalHistory(sbInPatientDiagnosisVoResponse.getPersonalHistory());
        medicalInhospitalDiagnosis.setFamilyHistory(sbInPatientDiagnosisVoResponse.getFamilyHistory());
        medicalInhospitalDiagnosis.setAllergyHistory(sbInPatientDiagnosisVoResponse.getAllergyHistory());
        medicalInhospitalDiagnosis.setMenstruationHistory(sbInPatientDiagnosisVoResponse.getMenstruationHistory());
        medicalInhospitalDiagnosis.setPhysicalExaminatio(sbInPatientDiagnosisVoResponse.getPhysicalExamination());
        medicalInhospitalDiagnosis.setSpecialistExamination(sbInPatientDiagnosisVoResponse.getSpecialistExamination());
        medicalInhospitalDiagnosis.setAuxiliaryExamination(sbInPatientDiagnosisVoResponse.getAuxiliaryExamination());
        medicalInhospitalDiagnosis.setTreatreCommend(sbInPatientDiagnosisVoResponse.getTreatRecommend());
        medicalInhospitalDiagnosis.setOutReason(conveterStringToShort(sbInPatientDiagnosisVoResponse.getOutReason()));
        medicalInhospitalDiagnosis.setOutDiseaseCode(sbInPatientDiagnosisVoResponse.getOutDiseaseCode());
        medicalInhospitalDiagnosis.setOutDiseaseName(sbInPatientDiagnosisVoResponse.getOutDiseaseName());
        medicalInhospitalDiagnosis.setOutAdvice(sbInPatientDiagnosisVoResponse.getOutAdvice());
        medicalInhospitalDiagnosis.setOutMedicalDiagnosisCode(sbInPatientDiagnosisVoResponse.getOutMedicalDiagnosisCode());
        medicalInhospitalDiagnosis.setOutMedicalDiagnosisName(sbInPatientDiagnosisVoResponse.getOutMedicalDiagnosisName());
        medicalInhospitalDiagnosis.setComplication(sbInPatientDiagnosisVoResponse.getComplication());
        medicalInhospitalDiagnosis.setInIcuTime(sbInPatientDiagnosisVoResponse.getInIcuTime());
        medicalInhospitalDiagnosis.setOutIcuTime(sbInPatientDiagnosisVoResponse.getOutIcuTime());
        medicalInhospitalDiagnosis.setSecondDiagnosisCode1(sbInPatientDiagnosisVoResponse.getSecondDiagnosisCode1());
        medicalInhospitalDiagnosis.setSecondDiagnosisName1(sbInPatientDiagnosisVoResponse.getSecondDiagnosisName1());
        medicalInhospitalDiagnosis.setSecondDiagnosisCode2(sbInPatientDiagnosisVoResponse.getSecondDiagnosisCode2());
        medicalInhospitalDiagnosis.setSecondDiagnosisName2(sbInPatientDiagnosisVoResponse.getSecondDiagnosisName2());
        medicalInhospitalDiagnosis.setSecondDiagnosisCode3(sbInPatientDiagnosisVoResponse.getSecondDiagnosisCode3());
        medicalInhospitalDiagnosis.setSecondDiagnosisName3(sbInPatientDiagnosisVoResponse.getSecondDiagnosisName3());
        medicalInhospitalDiagnosis.setSecondDiagnosisCode4(sbInPatientDiagnosisVoResponse.getSecondDiagnosisCode4());
        medicalInhospitalDiagnosis.setSecondDiagnosisName4(sbInPatientDiagnosisVoResponse.getSecondDiagnosisName4());
        medicalInhospitalDiagnosis.setInDays(conveterStringToShort(sbInPatientDiagnosisVoResponse.getInDays()));
        medicalInhospitalDiagnosis.setElectronicMedicalRecords(sbInPatientDiagnosisVoResponse.getElectronicMedicalRecords());
        medicalInhospitalDiagnosis.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalInhospitalDiagnosis;
    }

    private static String trimValue(String value) {
        String result = null;
        if (value != null) {
            result = StringUtils.isBlank(value.trim()) ? null : value.trim();
        }
        return result;
    }


    public ResponseObject getInhospitalFeeInfo(InPatientSumFeeListVo inPatientSumFeeListVo) {
        ResponseObject responseObject = new ResponseObject();
        try {
            MedicalInhospitalDiagnosis queryMedicalInhospitalDiagnosis = new MedicalInhospitalDiagnosis();
            queryMedicalInhospitalDiagnosis.setAdmissionNo(inPatientSumFeeListVo.getAdmissionNo());
            queryMedicalInhospitalDiagnosis.setInTime(conveterStringToInt(inPatientSumFeeListVo.getInTime()));
            queryMedicalInhospitalDiagnosis.setInpatientId(inPatientSumFeeListVo.getInPatientId());
            //offset = 2 的是出院诊断信息，offset= 1 的是入院诊信息只有是出院诊断信息才会有出入院时间
            queryMedicalInhospitalDiagnosis.setCurrentOffset(2);
            List<MedicalInhospitalDiagnosis> medicalInhospitalDiagnosisList = medicalInhospitalDiagnosisDao.select(queryMedicalInhospitalDiagnosis);
            logger.info("获取商保住院总费用清单的出院诊断信息 medicalInhospitalDiagnosisList ={}", new Object[]{JsonUtils.toJsonString(medicalInhospitalDiagnosisList)});
            if (CollectionUtils.isEmpty(medicalInhospitalDiagnosisList)) {
                responseObject.setResultCodeAndMessage("1001", "查询不到已经出院病人诊断信息");
                return responseObject;
            }
            MedicalInhospitalDiagnosis medicalInhospitalDiagnosis = medicalInhospitalDiagnosisList.get(0);

            //查看该就诊人有没有获取过费用信息

            MedicalPatient medicalPatient = medicalPatientDao.selectById(medicalInhospitalDiagnosis.getMedicalPatientId());

            if (medicalPatient != null && medicalPatient.getDownLoadFeeFlag() == 1) {
                logger.info("已经获取商保住院总费用清单的就诊人信息", new Object[]{JsonUtils.toJsonString(medicalPatient)});
                responseObject.setResultCodeAndMessage("0", "获取商保住院总费用清单成功");
                return responseObject;
            }

            inPatientSumFeeListVo.setBeginCostTime(medicalInhospitalDiagnosis.getInPatientTime());
            inPatientSumFeeListVo.setEndCostTime(medicalInhospitalDiagnosis.getOutPatientTime());
            logger.info("获取商保住院总费用清单his入参inPatientSumFeeListVo={}", new Object[]{JsonUtils.toJsonString(inPatientSumFeeListVo)});
            ResCommon<InPatientSumFeeListVoResponse> hisRespose = HisifaceService.getHisifaceClient().getInPatientSumFeeList(inPatientSumFeeListVo);
            logger.info("获取商保住院总费用清单his出参hisRespose ={}", new Object[]{JsonUtils.toJsonString(hisRespose)});
            if (!"0".equals(hisRespose.getResultCode())) {
                medicalPatient.setDownLoadFeeExceptionMessage(hisRespose.getResultMessage());
                medicalPatientDao.update(medicalPatient);
                responseObject.setResultCodeAndMessage("1001", "查询his住院总清单失败");
                return responseObject;
            }

            InPatientSumFeeListVoResponse inPatientSumFeeListVoResponse = hisRespose.getResult();
            if (inPatientSumFeeListVoResponse == null) {
                medicalPatient.setDownLoadFeeExceptionMessage("失败，住院总清单为空");
                medicalPatientDao.update(medicalPatient);
                responseObject.setResultCodeAndMessage("1001", "失败，住院总清单为空");
                return responseObject;
            }

            MedicalInhospitalFee medicalInhospitalFee = buildMedicalInhospitalFee(inPatientSumFeeListVoResponse);
            medicalInhospitalFee.setTotalRows(1);
            medicalInhospitalFee.setCurrentOffset(1);
            medicalInhospitalFee.setUploadFlag(new Short("0"));
            medicalInhospitalFee.setUploadTimes(0);
            medicalInhospitalFee.setOrgCode(inPatientSumFeeListVo.getHospitalCode());
            medicalInhospitalFee.setCaseNo(medicalInhospitalDiagnosis.getCaseNo());
            medicalInhospitalFee.setMedicalPatientId(medicalInhospitalDiagnosis.getMedicalPatientId());
            medicalInhospitalFeeDao.insert(medicalInhospitalFee);

            List<InPatientSumFeeDetail> subItems = inPatientSumFeeListVoResponse.getSubItems();
            if (CollectionUtils.isNotEmpty(subItems)) {
                List<MedicalInhospitalFeeDetail> medicalInhospitalFeeDetails = new ArrayList<>();
                int offset = 1;
                for (InPatientSumFeeDetail inPatientFeeDetail : subItems) {
                    MedicalInhospitalFeeDetail medicalInhospitalFeeDetail = buildMedicalInhospitalFeeDetail(inPatientFeeDetail);
                    medicalInhospitalFeeDetail.setOrgCode(inPatientSumFeeListVo.getHospitalCode());
                    medicalInhospitalFeeDetail.setCaseNo(medicalInhospitalDiagnosis.getCaseNo());
                    medicalInhospitalFeeDetail.setUploadFlag(new Short("0"));
                    medicalInhospitalFeeDetail.setUploadTimes(0);
                    medicalInhospitalFeeDetail.setTotalRows(subItems.size());
                    medicalInhospitalFeeDetail.setCurrentOffset(offset++);
                    medicalInhospitalFeeDetail.setMedicalPatientId(medicalInhospitalDiagnosis.getMedicalPatientId());
                    medicalInhospitalFeeDetail.setMedicalInhospitalFeeId(medicalInhospitalFee.getId());
                    medicalInhospitalFeeDetail.setInpatientId(inPatientSumFeeListVoResponse.getInPatientId());
                    medicalInhospitalFeeDetails.add(medicalInhospitalFeeDetail);
                }
                medicalInhospitalFeeDetailDao.batchInsert(medicalInhospitalFeeDetails);
            }
            medicalPatient.setDownLoadFeeFlag(new Short("1"));
            medicalPatientDao.update(medicalPatient);
        } catch (Exception e) {
            responseObject.setResultCodeAndMessage("-1", "系统异常");
            return responseObject;
        }
        return responseObject;
    }

    private MedicalInhospitalFeeDetail buildMedicalInhospitalFeeDetail(InPatientSumFeeDetail inPatientFeeDetail) {
        MedicalInhospitalFeeDetail medicalInhospitalFeeDetail = new MedicalInhospitalFeeDetail();
        medicalInhospitalFeeDetail.setId(PKGenerator.generateId());
        medicalInhospitalFeeDetail.setFeeType(converterStringToShort(inPatientFeeDetail.getFeeType()));
        medicalInhospitalFeeDetail.setInPatientFeeDetailId(inPatientFeeDetail.getInPatientFeeDetailId());
        medicalInhospitalFeeDetail.setReportId(inPatientFeeDetail.getReportId());
        medicalInhospitalFeeDetail.setFeeItemName(inPatientFeeDetail.getFeeItemName());
        medicalInhospitalFeeDetail.setFeeItemCode(inPatientFeeDetail.getFeeItemCode());
        medicalInhospitalFeeDetail.setFeeItemUnit(inPatientFeeDetail.getFeeItemUnit());
        medicalInhospitalFeeDetail.setFeeItemSpec(inPatientFeeDetail.getFeeItemSpec());
        medicalInhospitalFeeDetail.setFeeItemPrice(conveterStringToInt(inPatientFeeDetail.getFeeItemPrice()));
        medicalInhospitalFeeDetail.setFeeItemQty(inPatientFeeDetail.getFeeItemQty());
        medicalInhospitalFeeDetail.setFeeItemAmount(conveterStringToInt(inPatientFeeDetail.getFeeItemAmount()));
        medicalInhospitalFeeDetail.setTotalAmount(conveterStringToInt(inPatientFeeDetail.getTotalAmount()));
        medicalInhospitalFeeDetail.setSelfBurdenPercentage(inPatientFeeDetail.getSelfBurdenPercentage() == null ? null : Double.valueOf(inPatientFeeDetail.getSelfBurdenPercentage()));
        medicalInhospitalFeeDetail.setItemSpec(inPatientFeeDetail.getItemSpec());
        medicalInhospitalFeeDetail.setItemType(inPatientFeeDetail.getItemType());
        medicalInhospitalFeeDetail.setItemUnit(inPatientFeeDetail.getItemUnit());
        medicalInhospitalFeeDetail.setItemOrigin(inPatientFeeDetail.getItemOrigin());
        medicalInhospitalFeeDetail.setPackageUnit(inPatientFeeDetail.getPackageUnit());
        medicalInhospitalFeeDetail.setMiniDosageUnit(inPatientFeeDetail.getMiniDosageUnit());
        medicalInhospitalFeeDetail.setPerDose(inPatientFeeDetail.getPerDose());
        medicalInhospitalFeeDetail.setDosageUnit(inPatientFeeDetail.getDosageUnit());
        medicalInhospitalFeeDetail.setPerDayTimes(converterStringToShort(inPatientFeeDetail.getPerDayTimes()));
        medicalInhospitalFeeDetail.setMedicineDays(converterStringToShort(inPatientFeeDetail.getMedicineDays()));
        medicalInhospitalFeeDetail.setSingleCompoundFlag(inPatientFeeDetail.getSingleCompoundFlag());
        medicalInhospitalFeeDetail.setChineseHerbalMedicineQty(converterStringToShort(inPatientFeeDetail.getChineseHerbalMedicineQty()));
        medicalInhospitalFeeDetail.setItemLimitPrice(conveterStringToInt(inPatientFeeDetail.getItemLimitPrice()));
        medicalInhospitalFeeDetail.setApprovalNo(inPatientFeeDetail.getApprovalNo());
        medicalInhospitalFeeDetail.setSelfFeeFlag(inPatientFeeDetail.getSelfFeeFlag());
        medicalInhospitalFeeDetail.setSpecialMedicineFlag(inPatientFeeDetail.getSpecialMedicineFlag());
        medicalInhospitalFeeDetail.setInsuranceAuxiliaryInfo(inPatientFeeDetail.getInsuranceAuxiliaryInfo());
        medicalInhospitalFeeDetail.setSingleQty(converterStringToShort(inPatientFeeDetail.getSingleQty()));
        medicalInhospitalFeeDetail.setFrequencyValue(conveterStringToInt(inPatientFeeDetail.getFrequencyValue()));
        medicalInhospitalFeeDetail.setMedicineMethod(inPatientFeeDetail.getMedicineMethod());
        medicalInhospitalFeeDetail.setFrequencyCode(inPatientFeeDetail.getFrequencyCode());
        medicalInhospitalFeeDetail.setItemCode(inPatientFeeDetail.getItemCode());
        medicalInhospitalFeeDetail.setDrugSn(inPatientFeeDetail.getDrugSn());
        medicalInhospitalFeeDetail.setDrugOrigin(inPatientFeeDetail.getDrugOrigin());
        medicalInhospitalFeeDetail.setDrugSpecQty(conveterStringToInt(inPatientFeeDetail.getDrugSpecQty()));
        medicalInhospitalFeeDetail.setItemExecDate(inPatientFeeDetail.getItemExecDate());
        medicalInhospitalFeeDetail.setDoctorCode(inPatientFeeDetail.getDoctorCode());
        medicalInhospitalFeeDetail.setDoctorName(inPatientFeeDetail.getDoctorName());
        medicalInhospitalFeeDetail.setCombination(inPatientFeeDetail.getCombination());
        medicalInhospitalFeeDetail.setFunctions(inPatientFeeDetail.getFunctions());
        medicalInhospitalFeeDetail.setContraindication(inPatientFeeDetail.getContraindication());
        medicalInhospitalFeeDetail.setRecipeFlag(inPatientFeeDetail.getRecipeFlag());
        medicalInhospitalFeeDetail.setRestrictionsFlag(inPatientFeeDetail.getRestrictionsFlag());
        medicalInhospitalFeeDetail.setRestrictions(inPatientFeeDetail.getRestrictions());
        medicalInhospitalFeeDetail.setBaseDrugIdentification(inPatientFeeDetail.getBaseDrugIdentification());
        medicalInhospitalFeeDetail.setNationalMedicineId(inPatientFeeDetail.getNationalMedicineId());
        medicalInhospitalFeeDetail.setRegId(inPatientFeeDetail.getRegId());
        medicalInhospitalFeeDetail.setBrand(inPatientFeeDetail.getBrand());
        medicalInhospitalFeeDetail.setItemEnglishName(inPatientFeeDetail.getItemEnglishName());
        medicalInhospitalFeeDetail.setInsuranceRange(inPatientFeeDetail.getInsuranceRange());
        medicalInhospitalFeeDetail.setMaxLimitPrice1(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice1()));
        medicalInhospitalFeeDetail.setMaxLimitPrice2(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice2()));
        medicalInhospitalFeeDetail.setMaxLimitPrice3(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice3()));
        medicalInhospitalFeeDetail.setMaxLimitPrice4(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice4()));
        medicalInhospitalFeeDetail.setInsuranceSettleRangeAmount(conveterStringToInt(inPatientFeeDetail.getInsuranceSettleRangeAmount()));
        medicalInhospitalFeeDetail.setManufacturer(inPatientFeeDetail.getManufacturer());
        medicalInhospitalFeeDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalInhospitalFeeDetail;
    }

    private MedicalInhospitalFee buildMedicalInhospitalFee(InPatientSumFeeListVoResponse inPatientSumFeeListVoResponse) {
        MedicalInhospitalFee medicalInhospitalFee = new MedicalInhospitalFee();
        medicalInhospitalFee.setId(PKGenerator.generateId());
        medicalInhospitalFee.setInpatientId(inPatientSumFeeListVoResponse.getInPatientId());
        medicalInhospitalFee.setRefundFeeId(inPatientSumFeeListVoResponse.getRefundFeeId());
        medicalInhospitalFee.setAdmissionNo(inPatientSumFeeListVoResponse.getAdmissionNo());
        medicalInhospitalFee.setSocialSecurityNo(inPatientSumFeeListVoResponse.getSocialSecurityNo());
        medicalInhospitalFee.setInsuranceType(inPatientSumFeeListVoResponse.getInsuranceType());
        medicalInhospitalFee.setInvoiceNo(inPatientSumFeeListVoResponse.getInvoiceNo());
        medicalInhospitalFee.setListType(conveterStringToShort(inPatientSumFeeListVoResponse.getListType()));
        medicalInhospitalFee.setMedicalFeeSettleCode(inPatientSumFeeListVoResponse.getMedicalFeeSettleCode());
        medicalInhospitalFee.setMedicalFeeSettleName(inPatientSumFeeListVoResponse.getMedicalFeeSettleName());
        medicalInhospitalFee.setInvoiceTotalAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getInvoiceTotalAmount()));
        medicalInhospitalFee.setSelfAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getSelfAmount()));
        medicalInhospitalFee.setLargeAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getLargeAmount()));
        medicalInhospitalFee.setFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getFundAmount()));
        medicalInhospitalFee.setPastYearPayAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getPastYearPayAmount()));
        medicalInhospitalFee.setCurYearPayAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getCurYearPayAmount()));
        medicalInhospitalFee.setCivilServantSubsidyAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getCivilServantSubsidyAmount()));
        medicalInhospitalFee.setPerBurdenAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getPerBurdenAmount()));
        medicalInhospitalFee.setCashAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getCashAmount()));
        medicalInhospitalFee.setCurYearBalance(conveterStringToInt(inPatientSumFeeListVoResponse.getCurYearBalance()));
        medicalInhospitalFee.setPastYearPayAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getPastYearPayAmount()));
        medicalInhospitalFee.setSeriousIllnessComplianceAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getSeriousIllnessComplianceAmount()));
        medicalInhospitalFee.setRescueFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getLamorFundAmount()));
        medicalInhospitalFee.setHealthFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getHealthFundAmount()));
        medicalInhospitalFee.setPromptMessage(inPatientSumFeeListVoResponse.getPromptMessage());
        medicalInhospitalFee.setOldWorkerFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getOldWorkerFundAmount()));
        medicalInhospitalFee.setHospitalCostAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getHospitalCostAmount()));
        medicalInhospitalFee.setOtherFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getOtherFundAmount()));
        medicalInhospitalFee.setCollectionTime(inPatientSumFeeListVoResponse.getCollectionTime());
        medicalInhospitalFee.setCollectionCompany(inPatientSumFeeListVoResponse.getCollectionCompany());
        medicalInhospitalFee.setCollector(inPatientSumFeeListVoResponse.getCollector());
        medicalInhospitalFee.setSelfCareAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getSelfCareAmount()));
        medicalInhospitalFee.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalInhospitalFee;
    }

    public void executeUploadMedicalOutPatientFeeMessage(MedicalOutpatientFee unuploadMedicalOutpatientFee) {
        try {
            logger.info("mzFeeId={},待重传门诊费用信息unuploadMedicalOutpatientFee={}", new Object[]{unuploadMedicalOutpatientFee.getMzFeeId(),JsonUtils.toJsonString(unuploadMedicalOutpatientFee)});
            MedicalOutpatientFeeVo medicalOutpatientFeeVo = new MedicalOutpatientFeeVo();
            String encryptData = Util.encryptMedicalOutpatientFeeVo(medicalOutpatientFeeVo.buildMedicalOutpatientFeeVo(unuploadMedicalOutpatientFee), Constant.ENCRYPT_KEY);
            SbRequest sbRequest = new SbRequest();
            sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_FEE);
            sbRequest.setEncryptData(encryptData);
            try {
                AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                logger.info("mzFeeId={},重传门诊费用信息上传支付宝响应参数 data ={}", new Object[]{unuploadMedicalOutpatientFee.getMzFeeId(),JsonUtils.toJsonString(response)});
                if (response.isSuccess()) {
                    logger.info("mzFeeId={},重传门诊费用信息上传成功");
                    unuploadMedicalOutpatientFee.setUploadFlag(new Short("1"));
                    unuploadMedicalOutpatientFee.setUploadTimes(unuploadMedicalOutpatientFee.getUploadTimes() + 1);
                } else {
                    logger.info("门诊费用信息上传失败");
                    unuploadMedicalOutpatientFee.setExceptionMessage(response.getSubMsg());
                    unuploadMedicalOutpatientFee.setUploadTimes(unuploadMedicalOutpatientFee.getUploadTimes() + 1);
                }
                medicalOutpatientFeeDao.update(unuploadMedicalOutpatientFee);
            } catch (Exception e) {
                logger.error("门诊费用信息上传系统异常", e);
                unuploadMedicalOutpatientFee.setUploadTimes(unuploadMedicalOutpatientFee.getUploadTimes() + 1);
                unuploadMedicalOutpatientFee.setExceptionMessage(e.getMessage());
                medicalOutpatientFeeDao.update(unuploadMedicalOutpatientFee);
            }
        } catch (Exception e) {
            logger.error("门诊费用信息上传系统异常", e);
        }
    }


    public void executeUploadMedicalOutPatientFeeDetailMessage(MedicalOutpatientFeeDetail medicalOutpatientFeeDetail) {
        try {
            logger.info("mzFeeId={},待重传的门诊商保费用明细信息 medicalOutpatientFeeDetail ={}", new Object[]{medicalOutpatientFeeDetail.getMzFeeId(),JsonUtils.toJsonString(medicalOutpatientFeeDetail)});
            MedicalOutpatientFeeDetailVo medicalOutpatientFeeDetailVo = new MedicalOutpatientFeeDetailVo();
            String encryptData = Util.encryptMedicalOutpatientFeeDetailVo(medicalOutpatientFeeDetailVo.buildMedicalOutpatientFeeDetailVo(medicalOutpatientFeeDetail), Constant.ENCRYPT_KEY);
            SbRequest sbRequest = new SbRequest();
            sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_FEE_DETAIL);
            sbRequest.setEncryptData(encryptData);
            try {
                AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                logger.info("mzFeeId={},重传门诊商保费用明细信息上传支付宝响应参数 data ={}", new Object[]{medicalOutpatientFeeDetail.getMzFeeId(),JsonUtils.toJsonString(response)});
                if (response.isSuccess()) {
                    logger.info("mzFeeId={},重传门诊商保费用明细信息上传成功",medicalOutpatientFeeDetail.getMzFeeId());
                    medicalOutpatientFeeDetail.setUploadFlag(new Short("1"));
                } else {
                    logger.info("门诊商保费用明细信息上传失败");
                    medicalOutpatientFeeDetail.setExceptionMessage(response.getSubMsg());
                }
                medicalOutpatientFeeDetail.setUploadTimes(medicalOutpatientFeeDetail.getUploadTimes() + 1);
                medicalOutpatientFeeDetailDao.update(medicalOutpatientFeeDetail);
            } catch (Exception e) {
                logger.error("门诊商保费用明细信息上传系统异常", e);
            }
        } catch (Exception e) {
            logger.error("门诊商保费用明细信息上传系统异常", e);
        }
    }


    /**
     * 门诊退费时再次获取新的费用信息
     *
     * @param medicalPatientExtendVo
     */
    public void getMedicalFeeRecords(MedicalPatient medicalPatientExtendVo) {
        try {
            SBMZFeeRecordsVo feeRecordsVo = new SBMZFeeRecordsVo();
            feeRecordsVo.setMzFeeId(medicalPatientExtendVo.getMzFeeId());
            feeRecordsVo.setHospitalCode(medicalPatientExtendVo.getOrgCode());
            feeRecordsVo.setDeptCode(medicalPatientExtendVo.getVisitDeptCode());
            feeRecordsVo.setDoctorCode(medicalPatientExtendVo.getDiagnosisDoctorCode());
            logger.info("门诊退款获取商保门诊费用信息入参 feeRecordsVo ={}", new Object[]{JsonUtils.toJsonString(feeRecordsVo)});
            ResCommon<List<SBMZFeeRecordsVoResponse>> hisfiaceMzFeeRecordsResponse = HisifaceService.getHisifaceClient().getSBMZFeeRecords(feeRecordsVo);
            logger.info("门诊退款获取商保门诊费用信息出参 hisfiaceMzFeeRecordsResponse ={}", new Object[]{JsonUtils.toJsonString(hisfiaceMzFeeRecordsResponse)});
            List<SBMZFeeRecordsVoResponse> hisSBMZFeeRecordsVoResponseList = hisfiaceMzFeeRecordsResponse.getResult();
            if (!"0".equals(hisfiaceMzFeeRecordsResponse.getResultCode())) {
                medicalPatientExtendVo.setDownLoadFeeExceptionMessage(hisfiaceMzFeeRecordsResponse.getResultMessage());
                medicalPatientExtendVo.setDownLoadFeeTimes(medicalPatientExtendVo.getDownLoadFeeTimes() + 1);
                medicalPatientDao.update(medicalPatientExtendVo);
            }

            if (CollectionUtils.isNotEmpty(hisSBMZFeeRecordsVoResponseList)) {
                List<MedicalOutpatientFee> medicalOutpatientFeeList = new ArrayList<>();
                List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList = new ArrayList<>();
                int feeOffSet = 1;
                for (SBMZFeeRecordsVoResponse sBMZFeeRecordsVoResponse : hisSBMZFeeRecordsVoResponseList) {
                    MedicalOutpatientFee medicalOutpatientFee = buildMedicalOutPatientFee(sBMZFeeRecordsVoResponse);
                    medicalOutpatientFee.setCaseNo(medicalPatientExtendVo.getCaseNo());
                    medicalOutpatientFee.setMedicalPatientId(medicalPatientExtendVo.getId());
                    medicalOutpatientFee.setMzFeeId(medicalPatientExtendVo.getMzFeeId());
                    medicalOutpatientFee.setCurrentOffset(feeOffSet);
                    medicalOutpatientFee.setTotalRows(hisSBMZFeeRecordsVoResponseList.size());
                    medicalOutpatientFeeList.add(medicalOutpatientFee);

                    List<SBMZFeeDetail> subItems = sBMZFeeRecordsVoResponse.getSubItems();
                    if (CollectionUtils.isNotEmpty(subItems)) {
                        for (SBMZFeeDetail sBMZFeeDetail : subItems) {
                            MedicalOutpatientFeeDetail medicalOutpatientFeeDetail = buildMedicalOutPatientFeeDetail(sBMZFeeDetail);
                            medicalOutpatientFeeDetail.setCaseNo(medicalPatientExtendVo.getCaseNo());
                            medicalOutpatientFeeDetail.setPayNo(medicalOutpatientFee.getPayNo());
                            medicalOutpatientFeeDetail.setMedicalPatientId(medicalPatientExtendVo.getId());
                            medicalOutpatientFeeDetail.setMedicalOutpatientFeeId(medicalOutpatientFee.getId());
                            medicalOutpatientFeeDetail.setMzFeeId(medicalPatientExtendVo.getMzFeeId());
                            medicalOutpatientFeeDetailList.add(medicalOutpatientFeeDetail);
                        }
                    }
                    feeOffSet++;
                }

                int feeDetailOffSet = 1;
                for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                    medicalOutpatientFeeDetail.setCurrentOffset(feeDetailOffSet++);
                    medicalOutpatientFeeDetail.setTotalRows(medicalOutpatientFeeDetailList.size());
                }

                medicalOutpatientFeeDetailDao.batchInsert(medicalOutpatientFeeDetailList);
                medicalOutpatientFeeDao.batchInsert(medicalOutpatientFeeList);
                medicalPatientExtendVo.setDownLoadFeeFlag(new Short("1"));
                medicalPatientExtendVo.setDownLoadFeeTimes(medicalPatientExtendVo.getDownLoadFeeTimes() + 1);


                medicalPatientDao.update(medicalPatientExtendVo);
                for (MedicalOutpatientFee medicalOutpatientFee : medicalOutpatientFeeList) {
                    executeUploadMedicalOutPatientFeeMessage(medicalOutpatientFee);
                }
                for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                    executeUploadMedicalOutPatientFeeDetailMessage(medicalOutpatientFeeDetail);
                }
            }
        } catch (Exception e) {
            logger.info("门诊退款获取商保门诊费用信息异常", e);
        }
    }

    private MedicalOutpatientFeeDetail buildMedicalOutPatientFeeDetail(SBMZFeeDetail sBMZFeeDetail) {
        MedicalOutpatientFeeDetail medicalOutpatientFeeDetail = new MedicalOutpatientFeeDetail();
        medicalOutpatientFeeDetail.setId(PKGenerator.generateId());
        medicalOutpatientFeeDetail.setMzFeeDetailId(sBMZFeeDetail.getMzFeeDetailId());
        medicalOutpatientFeeDetail.setReportId(sBMZFeeDetail.getReportId());
        medicalOutpatientFeeDetail.setFeeType(new Short(sBMZFeeDetail.getFeeType()));
        medicalOutpatientFeeDetail.setFeeItemName(sBMZFeeDetail.getFeeItemName());
        medicalOutpatientFeeDetail.setFeeItemCode(sBMZFeeDetail.getFeeItemCode());
        medicalOutpatientFeeDetail.setFeeItemUnit(sBMZFeeDetail.getFeeItemUnit());
        medicalOutpatientFeeDetail.setFeeItemSpec(sBMZFeeDetail.getFeeItemSpec());
        medicalOutpatientFeeDetail.setFeeItemPrice(conveterStringToInt(sBMZFeeDetail.getFeeItemPrice()));
        medicalOutpatientFeeDetail.setFeeItemQty(sBMZFeeDetail.getFeeItemQty());
        medicalOutpatientFeeDetail.setFeeItemAmount(conveterStringToInt(sBMZFeeDetail.getFeeItemAmount()));
        medicalOutpatientFeeDetail.setTotalAmount(conveterStringToInt(sBMZFeeDetail.getTotalAmount()));
        medicalOutpatientFeeDetail.setSelfBurdenPercentage(sBMZFeeDetail.getSelfBurdenPercentage() == null ? null : Double.valueOf(sBMZFeeDetail.getSelfBurdenPercentage()));
        medicalOutpatientFeeDetail.setItemSpec(sBMZFeeDetail.getItemSpec());
        medicalOutpatientFeeDetail.setItemType(sBMZFeeDetail.getItemType());
        medicalOutpatientFeeDetail.setItemUnit(sBMZFeeDetail.getItemUnit());
        medicalOutpatientFeeDetail.setItemOrigin(sBMZFeeDetail.getItemOrigin());
        medicalOutpatientFeeDetail.setPackageUnit(sBMZFeeDetail.getPackageUnit());
        medicalOutpatientFeeDetail.setMiniDosageUnit(sBMZFeeDetail.getMiniDosageUnit());
        medicalOutpatientFeeDetail.setPerDose(sBMZFeeDetail.getPerDose());
        medicalOutpatientFeeDetail.setDosageUnit(sBMZFeeDetail.getDosageUnit());
        medicalOutpatientFeeDetail.setPerDayTimes(sBMZFeeDetail.getPerDayTimes());
        medicalOutpatientFeeDetail.setMedicineDays(sBMZFeeDetail.getMedicineDays());
        medicalOutpatientFeeDetail.setSingleCompoundFlag(sBMZFeeDetail.getSingleCompoundFlag());
        medicalOutpatientFeeDetail.setChineseHerbalMedicineQty(sBMZFeeDetail.getChineseHerbalMedicineQty());
        medicalOutpatientFeeDetail.setItemLimitPrice(conveterStringToInt(sBMZFeeDetail.getItemLimitPrice()));
        medicalOutpatientFeeDetail.setApprovalNo(sBMZFeeDetail.getApprovalNo());
        medicalOutpatientFeeDetail.setSelfFeeFlag(sBMZFeeDetail.getSelfFeeFlag());
        medicalOutpatientFeeDetail.setSpecialMedicineFlag(sBMZFeeDetail.getSpecialMedicineFlag());
        medicalOutpatientFeeDetail.setInsuranceAuxiliaryInfo(sBMZFeeDetail.getInsuranceAuxiliaryInfo());
        medicalOutpatientFeeDetail.setSingleQty(sBMZFeeDetail.getSingleQty());
        medicalOutpatientFeeDetail.setFrequencyValue(sBMZFeeDetail.getFrequencyValue());
        medicalOutpatientFeeDetail.setMedicineMethod(sBMZFeeDetail.getMedicineMethod());
        medicalOutpatientFeeDetail.setFrequencyCode(sBMZFeeDetail.getFrequencyCode());
        medicalOutpatientFeeDetail.setItemCode(sBMZFeeDetail.getItemCode());
        medicalOutpatientFeeDetail.setDrugSn(sBMZFeeDetail.getDrugSn());
        medicalOutpatientFeeDetail.setDrugOrigin(sBMZFeeDetail.getDrugOrigin());
        medicalOutpatientFeeDetail.setDrugSpecQty(sBMZFeeDetail.getDrugSpecQty());
        medicalOutpatientFeeDetail.setItemExecDate(sBMZFeeDetail.getItemExecDate());
        medicalOutpatientFeeDetail.setDoctorCode(sBMZFeeDetail.getDoctorCode());
        medicalOutpatientFeeDetail.setDoctorName(sBMZFeeDetail.getDoctorName());
        medicalOutpatientFeeDetail.setCombination(sBMZFeeDetail.getCombination());
        medicalOutpatientFeeDetail.setFunctions(sBMZFeeDetail.getFunctions());
        medicalOutpatientFeeDetail.setContraindication(sBMZFeeDetail.getContraindication());
        medicalOutpatientFeeDetail.setRecipeFlag(sBMZFeeDetail.getRecipeFlag());
        medicalOutpatientFeeDetail.setRestrictionsFlag(sBMZFeeDetail.getRestrictionsFlag());
        medicalOutpatientFeeDetail.setRestrictions(sBMZFeeDetail.getRestrictions());
        medicalOutpatientFeeDetail.setBaseDrugIdentification(sBMZFeeDetail.getBaseDrugIdentification());
        medicalOutpatientFeeDetail.setNationalMedicineId(sBMZFeeDetail.getNationalMedicineId());
        medicalOutpatientFeeDetail.setRegId(sBMZFeeDetail.getRegId());
        medicalOutpatientFeeDetail.setBrand(sBMZFeeDetail.getBrand());
        medicalOutpatientFeeDetail.setItemEnglishName(sBMZFeeDetail.getItemEnglishName());
        medicalOutpatientFeeDetail.setInsuranceRange(sBMZFeeDetail.getInsuranceRange());
        medicalOutpatientFeeDetail.setMaxLimitPrice1(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice1()));
        medicalOutpatientFeeDetail.setMaxLimitPrice2(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice2()));
        medicalOutpatientFeeDetail.setMaxLimitPrice3(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice3()));
        medicalOutpatientFeeDetail.setMaxLimitPrice4(conveterStringToInt(sBMZFeeDetail.getMaxLimitPrice4()));
        medicalOutpatientFeeDetail.setInsuranceSettleRangeAmount(conveterStringToInt(sBMZFeeDetail.getInsuranceSettleRangeAmount()));
        medicalOutpatientFeeDetail.setManufacturer(sBMZFeeDetail.getManufacturer());
        // TODO: 2017/9/11
        medicalOutpatientFeeDetail.setOrgCode(HOSPITAL_CODE);
        medicalOutpatientFeeDetail.setUploadTimes(0);
        medicalOutpatientFeeDetail.setUploadFlag(new Short("0"));
        medicalOutpatientFeeDetail.setDownLoadReportFlag(new Short("0"));
        medicalOutpatientFeeDetail.setDownLoadReportTimes(0);
        medicalOutpatientFeeDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalOutpatientFeeDetail;
    }


    private MedicalOutpatientFee buildMedicalOutPatientFee(SBMZFeeRecordsVoResponse sBMZFeeRecordsVoResponse) {
        MedicalOutpatientFee medicalOutpatientFee = new MedicalOutpatientFee();
        medicalOutpatientFee.setId(PKGenerator.generateId());
        medicalOutpatientFee.setMzFeeId(sBMZFeeRecordsVoResponse.getMzFeeId());
        medicalOutpatientFee.setReFundFeeId(sBMZFeeRecordsVoResponse.getRefundFeeId());
        medicalOutpatientFee.setSocialSecurityNo(sBMZFeeRecordsVoResponse.getSocialSecurityNo());
        medicalOutpatientFee.setInsuranceType(sBMZFeeRecordsVoResponse.getInsuranceType());
        medicalOutpatientFee.setInvoiceNo(sBMZFeeRecordsVoResponse.getInvoiceNo());
        medicalOutpatientFee.setListType(conveterStringToShort(sBMZFeeRecordsVoResponse.getListType()));
        medicalOutpatientFee.setMedicalFeeSettleCode(sBMZFeeRecordsVoResponse.getMedicalFeeSettleCode());
        medicalOutpatientFee.setInvoiceTotalAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getInvoiceTotalAmount()));
        medicalOutpatientFee.setSelfAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSelfAmount()));
        medicalOutpatientFee.setLargeAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSelfAmount()));
        medicalOutpatientFee.setFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getFundAmount()));
        medicalOutpatientFee.setCurYearPayAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getCurYearPayAmount()));
        medicalOutpatientFee.setCivilServantSubsidyAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getCivilServantSubsidyAmount()));
        medicalOutpatientFee.setPerBurdenAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getPerBurdenAmount()));
        medicalOutpatientFee.setCashAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getCashAmount()));
        medicalOutpatientFee.setCurYearBalance(conveterStringToInt(sBMZFeeRecordsVoResponse.getCurYearBalance()));
        medicalOutpatientFee.setPastYearPayAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getPastYearPayAmount()));
        medicalOutpatientFee.setSeriousIllnessComplianceAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSeriousIllnessComplianceAmount()));
        medicalOutpatientFee.setRescueFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getLamorFundAmount()));
        medicalOutpatientFee.setHealthFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getHealthFundAmount()));
        medicalOutpatientFee.setPromptMessage(sBMZFeeRecordsVoResponse.getPromptMessage());
        medicalOutpatientFee.setOldWorkerFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getOldWorkerFundAmount()));
        medicalOutpatientFee.setHospitalCostAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getHospitalCostAmount()));
        medicalOutpatientFee.setOtherFundAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getOtherFundAmount()));
        medicalOutpatientFee.setCollectionTime(sBMZFeeRecordsVoResponse.getCollectionTime());
        medicalOutpatientFee.setCollectionCompany(sBMZFeeRecordsVoResponse.getCollectionCompany());
        medicalOutpatientFee.setCollector(sBMZFeeRecordsVoResponse.getCollector());
        medicalOutpatientFee.setSelfCareAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getSelfCareAmount()));
        medicalOutpatientFee.setOverAllAmount(conveterStringToInt(sBMZFeeRecordsVoResponse.getOverallAmount()));
        medicalOutpatientFee.setPayNo(sBMZFeeRecordsVoResponse.getPayFeeId());
        //// TODO: 2017/9/8
        medicalOutpatientFee.setOrgCode(HOSPITAL_CODE);
        medicalOutpatientFee.setUploadTimes(0);
        medicalOutpatientFee.setUploadFlag(new Short("0"));
        medicalOutpatientFee.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalOutpatientFee;
    }

    public void executeUploadMedicalOutpatientDiagnosis(List<MedicalOutpatientDiagnosis> medicalOutpatientDiagnosisList) {
        try {
            for (MedicalOutpatientDiagnosis medicalOutpatientDiagnosis : medicalOutpatientDiagnosisList) {
                logger.info("mzFeeId={},待重传的门诊商保诊断信息 medicalOutpatientDiagnosis ={}", new Object[]{medicalOutpatientDiagnosis.getMzFeeId(),JsonUtils.toJsonString(medicalOutpatientDiagnosis)});
                MedicalOutpatientDiagnosisVo medicalOutpatientDiagnosisVo = new MedicalOutpatientDiagnosisVo();
                String encryptData = Util.encryptMedicalOutpatientDiagnosis(medicalOutpatientDiagnosisVo.buildMedicalOutpatientDiagnosisVo(medicalOutpatientDiagnosis), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_DIAGNOSIS);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("mzFeeId={},重传门诊商保诊断信息上传付宝响应参数 data ={}", new Object[]{medicalOutpatientDiagnosis.getMzFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("mzFeeId={}，重传门诊商保诊断信息上传成功",medicalOutpatientDiagnosis.getMzFeeId());
                        medicalOutpatientDiagnosis.setUploadFlag(new Short("1"));
                        medicalOutpatientDiagnosis.setUploadTimes(medicalOutpatientDiagnosis.getUploadTimes() + 1);
                        medicalOutpatientDiagnosisDao.update(medicalOutpatientDiagnosis);
                    } else {
                        logger.info("门诊商保诊断信息上传失败");
                        medicalOutpatientDiagnosis.setUploadTimes(medicalOutpatientDiagnosis.getUploadTimes() + 1);
                        medicalOutpatientDiagnosis.setExceptionMessage(response.getSubMsg());
                        medicalOutpatientDiagnosisDao.update(medicalOutpatientDiagnosis);
                    }
                } catch (Exception e) {
                    logger.error("门诊商保诊断信息上传系统异常", e);
                }
            }

        } catch (Exception e) {
            logger.error("门诊商保诊断信息上传系统异常", e);
        }
    }

    public void executeUploadMedicalOutPatientFeeMessage(List<MedicalOutpatientFee> medicalOutpatientFeeList) {
        try {
            for (MedicalOutpatientFee medicalOutpatientFee : medicalOutpatientFeeList) {
                logger.info("mzFeeId={},待重新上传的门诊商保费用信息 medicalOutpatientFee ={}", new Object[]{medicalOutpatientFee.getMzFeeId(),JsonUtils.toJsonString(medicalOutpatientFee)});
                MedicalOutpatientFeeVo medicalOutpatientFeeVo = new MedicalOutpatientFeeVo();
                String encryptData = Util.encryptMedicalOutpatientFeeVo(medicalOutpatientFeeVo.buildMedicalOutpatientFeeVo(medicalOutpatientFee), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_FEE);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("mzFeeId={},门诊商保费用信息上传支付宝响应参数 data ={}", new Object[]{medicalOutpatientFee.getMzFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("mzFeeId={},门诊商保费用信息上传成功",medicalOutpatientFee.getMzFeeId());
                        medicalOutpatientFee.setUploadFlag(new Short("1"));
                        medicalOutpatientFee.setUploadTimes(medicalOutpatientFee.getUploadTimes() + 1);
                    } else {
                        logger.info("门诊商保费用信息上传失败");
                        medicalOutpatientFee.setExceptionMessage(response.getSubMsg());
                        medicalOutpatientFee.setUploadTimes(medicalOutpatientFee.getUploadTimes() + 1);
                    }
                    medicalOutpatientFeeDao.update(medicalOutpatientFee);
                } catch (Exception e) {
                    logger.error("门诊商保费用信息上传系统异常", e);
                    medicalOutpatientFee.setUploadTimes(medicalOutpatientFee.getUploadTimes() + 1);
                    medicalOutpatientFee.setExceptionMessage(e.getMessage());
                    medicalOutpatientFeeDao.update(medicalOutpatientFee);
                }
            }

        } catch (Exception e) {
            logger.error("门诊商保费用信息上传系统异常", e);
        }
    }

    public void executeUploadMedicalOutPatientFeeDetailMessage(List<MedicalOutpatientFeeDetail> medicalOutpatientFeeDetailList) {
        try {
            for (MedicalOutpatientFeeDetail medicalOutpatientFeeDetail : medicalOutpatientFeeDetailList) {
                logger.info("mzFeeId={},待重传的门诊商保费用明细信息medicalOutpatientFeeDetail ={}", new Object[]{medicalOutpatientFeeDetail.getMzFeeId(),JsonUtils.toJsonString(medicalOutpatientFeeDetail)});
                MedicalOutpatientFeeDetailVo medicalOutpatientFeeDetailVo = new MedicalOutpatientFeeDetailVo();
                String encryptData = Util.encryptMedicalOutpatientFeeDetailVo(medicalOutpatientFeeDetailVo.buildMedicalOutpatientFeeDetailVo(medicalOutpatientFeeDetail), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_OUTPATIENT_FEE_DETAIL);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("mzFeeId={},门诊商保费用明细信息上传支付宝响应参数 response={}", new Object[]{medicalOutpatientFeeDetail.getMzFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("mzFeeId={},门诊商保费用明细信息上传成功",medicalOutpatientFeeDetail.getMzFeeId());
                        medicalOutpatientFeeDetail.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("门诊商保费用明细信息上传失败");
                        medicalOutpatientFeeDetail.setExceptionMessage(response.getSubMsg());
                    }
                    medicalOutpatientFeeDetail.setUploadTimes(medicalOutpatientFeeDetail.getUploadTimes() + 1);
                    medicalOutpatientFeeDetailDao.update(medicalOutpatientFeeDetail);
                } catch (Exception e) {
                    logger.error("门诊商保费用明细信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("门诊商保费用明细信息上传系统异常", e);
        }
    }

    public void executeUploadExaminationMessage(List<MedicalExamination> medicalExaminationList, String sceneCode) {
        try {
            for (MedicalExamination medicalExamination : medicalExaminationList) {
                logger.info("mzFeeId={},待重传的门诊检验信息medicalExamination ={}", new Object[]{medicalExamination.getBusinessFeeId(),JsonUtils.toJsonString(medicalExamination)});
                MedicalOutpatientExaminationVo medicalOutpatientExaminationVo = new MedicalOutpatientExaminationVo();
                String encryptData = Util.encryptMedicalOutpatientExaminationVo(medicalOutpatientExaminationVo.buildMedicalOutpatientExaminationVo(medicalExamination), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(sceneCode);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("mzFeeId={},门诊检验信息上传支付宝响应参数response={}", new Object[]{medicalExamination.getBusinessFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("mzFeeId={},门诊检验信息上传成功",medicalExamination.getBusinessFeeId());
                        medicalExamination.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("门诊检验信息上传失败");
                        medicalExamination.setExceptionMessage(response.getSubMsg());
                    }
                    medicalExamination.setUploadTimes(medicalExamination.getUploadTimes() + 1);
                    medicalExaminationDao.update(medicalExamination);
                } catch (Exception e) {
                    logger.error("门诊检验信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("门诊检验信息上传系统异常", e);
        }
    }

    public void executeUploadInhospitalExaminationMessage(List<MedicalExamination> medicalExaminationList, String sceneCode) {
        try {
            for (MedicalExamination medicalExamination : medicalExaminationList) {
                logger.info("mzFeeId={},待重传的门诊检验信息medicalExamination ={}", new Object[]{medicalExamination.getBusinessFeeId(),JsonUtils.toJsonString(medicalExamination)});
                MedicalInhospitalExaminationVo medicalInhospitalExaminationVo = new MedicalInhospitalExaminationVo();
                String encryptData = Util.encryptMedicalInhospitalExaminationVo(medicalInhospitalExaminationVo.buildMedicalInhospitalExaminationVo(medicalExamination), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(sceneCode);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("mzFeeId={},门诊检验信息上传支付宝响应参数 response={}", new Object[]{medicalExamination.getBusinessFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("mzFeeId={},门诊检验信息上传成功",medicalExamination.getBusinessFeeId());
                        medicalExamination.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("门诊检验信息上传失败");
                        medicalExamination.setExceptionMessage(response.getSubMsg());
                    }
                    medicalExamination.setUploadTimes(medicalExamination.getUploadTimes() + 1);
                    medicalExaminationDao.update(medicalExamination);
                } catch (Exception e) {
                    logger.error("门诊检验信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("门诊检验信息上传系统异常", e);
        }
    }


    public void executeUploadSurgeryMessage(List<MedicalOperationRecord> medicalOperationRecordList, String sceneCode) {
        try {
            for (MedicalOperationRecord medicalOperationRecord : medicalOperationRecordList) {
                logger.info("待重传门诊手术信息medicalOperationRecord={}", new Object[]{JsonUtils.toJsonString(medicalOperationRecord)});
                MedicalOutpatientOperationRecordVo medicalOutpatientOperationRecordVo = new MedicalOutpatientOperationRecordVo();
                String encryptData = Util.encryptMedicalOutpatientOperationRecordVo(medicalOutpatientOperationRecordVo.buildMedicalOutpatientOperationRecordVo(medicalOperationRecord), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(sceneCode);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("门诊手术信息上传支付宝响应参数 data ={}", new Object[]{JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("门诊手术信息上传成功");
                        medicalOperationRecord.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("门诊手术信息上传失败");
                        medicalOperationRecord.setExceptionMessage(response.getSubMsg());
                    }
                    medicalOperationRecord.setUploadTimes(medicalOperationRecord.getUploadTimes() + 1);
                    medicalOperationRecordDao.update(medicalOperationRecord);

                } catch (Exception e) {
                    logger.error("门诊手术信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("门诊手术信息上传系统异常", e);
        }
    }

    public void executeUploadInhospitalSurgeryMessage(List<MedicalOperationRecord> medicalOperationRecordList, String sceneCode) {
        try {
            for (MedicalOperationRecord medicalOperationRecord : medicalOperationRecordList) {
                logger.info("inPatientId={},待重传住院手术信息medicalOperationRecord={}", new Object[]{medicalOperationRecord.getInpatientId(),JsonUtils.toJsonString(medicalOperationRecord)});
                MedicalInhospitalOperationRecordVo medicalInhospitalOperationRecordVo = new MedicalInhospitalOperationRecordVo();
                String encryptData = Util.encryptMedicalInhospitalOperationRecordVo(medicalInhospitalOperationRecordVo.buildMedicalInhospitalOperationRecordVo(medicalOperationRecord), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(sceneCode);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("inPatientId={},重传住院手术信息上传支付宝响应参数 response={}", new Object[]{medicalOperationRecord.getInpatientId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("inPatientId={},重传住院手术信息上传成功",medicalOperationRecord.getInpatientId());
                        medicalOperationRecord.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("住院手术信息上传失败");
                        medicalOperationRecord.setExceptionMessage(response.getSubMsg());
                    }
                    medicalOperationRecord.setUploadTimes(medicalOperationRecord.getUploadTimes() + 1);
                    medicalOperationRecordDao.update(medicalOperationRecord);

                } catch (Exception e) {
                    logger.error("住院手术信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("住院手术信息上传系统异常", e);
        }
    }


    public void executeUploadCheckingMessage(List<MedicalChecking> uploadCheckingList, String sceneCode) {
        try {
            if (CollectionUtils.isNotEmpty(uploadCheckingList)) {
                for (MedicalChecking medicalChecking : uploadCheckingList) {
                    logger.info("待重传门诊检查信息 medicalChecking ={}", new Object[]{JsonUtils.toJsonString(medicalChecking)});
                    MedicalOutpatientCheckingVo medicalOutpatientFeeVo = new MedicalOutpatientCheckingVo();
                    String encryptData = Util.encryptMedicalChecking(medicalOutpatientFeeVo.buildMedicalOutpatientCheckingVo(medicalChecking), Constant.ENCRYPT_KEY);
                    SbRequest sbRequest = new SbRequest();
                    sbRequest.setSceneCode(sceneCode);
                    sbRequest.setEncryptData(encryptData);
                    try {
                        AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                        logger.info("mzFeeId={},重传门诊检查信息上传支付宝响应参数 response ={}", new Object[]{medicalChecking.getBusinessFeeId(),JsonUtils.toJsonString(response)});
                        if (response.isSuccess()) {
                            logger.info("mzFeeId={},重传门诊检查信息上传成功",medicalChecking.getBusinessFeeId());
                            medicalChecking.setUploadFlag(new Short("1"));
                            medicalChecking.setUploadTimes(medicalChecking.getUploadTimes() + 1);
                        } else {
                            logger.info("门诊检查信息上传失败");
                            medicalChecking.setUploadTimes(medicalChecking.getUploadTimes() + 1);
                            medicalChecking.setExceptionMessage(response.getSubMsg());
                        }
                        medicalCheckingDao.update(medicalChecking);
                    } catch (Exception e) {
                        logger.error("上传支付宝信息系统异常", e);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("上传支付宝信息系统异常", e);
        }
    }


    public void executeUploadInhospitalCheckingMessage(List<MedicalChecking> uploadCheckingList, String sceneCode) {
        try {
            if (CollectionUtils.isNotEmpty(uploadCheckingList)) {
                for (MedicalChecking medicalChecking : uploadCheckingList) {
                    logger.info("inPatientId={},待重传住院检查信息 medicalChecking ={}", new Object[]{medicalChecking.getBusinessFeeId(),JsonUtils.toJsonString(medicalChecking)});
                    MedicalInhospitalCheckingVo medicalInhospitalCheckingVo = new MedicalInhospitalCheckingVo();
                    String encryptData = Util.encryptMedicalInhospitalCheckingVo(medicalInhospitalCheckingVo.buildMedicalInhospitalCheckingVo(medicalChecking), Constant.ENCRYPT_KEY);
                    SbRequest sbRequest = new SbRequest();
                    sbRequest.setSceneCode(sceneCode);
                    sbRequest.setEncryptData(encryptData);
                    try {
                        AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                        logger.info("inPatientId={},重传住院检查信息上传支付宝响应参数 response ={}", new Object[]{medicalChecking.getBusinessFeeId(),JsonUtils.toJsonString(response)});
                        if (response.isSuccess()) {
                            logger.info("inPatientId={}，重传住院检查信息上传成功",medicalChecking.getBusinessFeeId());
                            medicalChecking.setUploadFlag(new Short("1"));
                            medicalChecking.setUploadTimes(medicalChecking.getUploadTimes() + 1);
                        } else {
                            logger.info("住院检查信息上传失败");
                            medicalChecking.setUploadTimes(medicalChecking.getUploadTimes() + 1);
                            medicalChecking.setExceptionMessage(response.getSubMsg());
                        }
                        medicalCheckingDao.update(medicalChecking);
                    } catch (Exception e) {
                        logger.error("上传支付宝信息系统异常", e);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("上传支付宝信息系统异常", e);
        }
    }


    public void executeUploadCheckingDetailMessage(List<MedicalCheckingDetail> uploadCheckingDetailList, String sceneCode) {
        try {
            for (MedicalCheckingDetail medicalCheckingDetail : uploadCheckingDetailList) {
                logger.info("mzFeeId={},待重传门诊检验明细medicalCheckingDetail ={}", new Object[]{medicalCheckingDetail.getBusinessFeeId(),JsonUtils.toJsonString(medicalCheckingDetail)});
                MedicalOutpatientCheckingDetailVo medicalOutpatientCheckingDetailVo = new MedicalOutpatientCheckingDetailVo();
                String encryptData = Util.encryptMedicalCheckingDetialVo(medicalOutpatientCheckingDetailVo.buildMedicalOutpatientCheckingDetailVo(medicalCheckingDetail), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(sceneCode);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("mzFeeId={},重传门诊检验明细上传支付宝响应参数 response ={}", new Object[]{medicalCheckingDetail.getBusinessFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("mzFeeId={},重传门诊检验明细上传成功",medicalCheckingDetail.getBusinessFeeId());
                        medicalCheckingDetail.setUploadTimes(medicalCheckingDetail.getUploadTimes() + 1);
                        medicalCheckingDetail.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("门诊检验明细上传失败");
                        medicalCheckingDetail.setUploadTimes(medicalCheckingDetail.getUploadTimes() + 1);
                        medicalCheckingDetail.setExceptionMessage(response.getSubMsg());
                    }
                    medicalCheckingDetailDao.update(medicalCheckingDetail);
                } catch (Exception e) {
                    logger.error("门诊检验明细上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("门诊检验明细上传系统异常", e);
        }
    }


    public void executeUploadInhospitalCheckingDetailMessage(List<MedicalCheckingDetail> uploadCheckingDetailList, String sceneCode) {
        try {
            for (MedicalCheckingDetail medicalCheckingDetail : uploadCheckingDetailList) {
                logger.info("inPatientId={},待重传住院检验明细medicalCheckingDetail ={}", new Object[]{medicalCheckingDetail.getBusinessFeeId(),JsonUtils.toJsonString(medicalCheckingDetail)});
                MedicalInhospitalCheckingDetailVo medicalInhospitalCheckingDetailVo = new MedicalInhospitalCheckingDetailVo();
                String encryptData = Util.encryptMedicalInhospitalCheckingDetailVo(medicalInhospitalCheckingDetailVo.buildMedicalInhospitalCheckingDetailVo(medicalCheckingDetail), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(sceneCode);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("inPatientId={},重传住院检验明细上传支付宝响应参数 response ={}", new Object[]{medicalCheckingDetail.getBusinessFeeId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("inPatientId={},重传住院检验明细上传成功",medicalCheckingDetail.getBusinessFeeId());
                        medicalCheckingDetail.setUploadTimes(medicalCheckingDetail.getUploadTimes() + 1);
                        medicalCheckingDetail.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("住院检验明细上传失败");
                        medicalCheckingDetail.setUploadTimes(medicalCheckingDetail.getUploadTimes() + 1);
                        medicalCheckingDetail.setExceptionMessage(response.getSubMsg());
                    }
                    medicalCheckingDetailDao.update(medicalCheckingDetail);
                } catch (Exception e) {
                    logger.error("住院检验明细上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("住院检验明细上传系统异常", e);
        }
    }


    public void executeUploadInhospitalDiagnosis(List<MedicalInhospitalDiagnosis> medicalInhospitalDiagnosisList) {
        try {
            if (CollectionUtils.isNotEmpty(medicalInhospitalDiagnosisList)) {
                for (MedicalInhospitalDiagnosis unuploadMedicalOutpatientDiagnosis : medicalInhospitalDiagnosisList) {
                    logger.info("inPatientId={},待重传上传的门诊商保诊断信息unuploadMedicalOutpatientDiagnosis ={}", new Object[]{unuploadMedicalOutpatientDiagnosis.getInpatientId(),JsonUtils.toJsonString(unuploadMedicalOutpatientDiagnosis)});
                    MedicalInhospitalDiagnosisVo medicalInhospitalDiagnosisVo = new MedicalInhospitalDiagnosisVo();
                    String encryptData = Util.encryptMedicalInhospitalDiagnosisVo(medicalInhospitalDiagnosisVo.buildMedicalInhospitalDiagnosisVo(unuploadMedicalOutpatientDiagnosis), Constant.ENCRYPT_KEY);
                    SbRequest sbRequest = new SbRequest();
                    sbRequest.setSceneCode(Constant.MEDICAL_INHOSPITAL_DIAGNOSIS);
                    sbRequest.setEncryptData(encryptData);
                    try {
                        AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                        logger.info("inPatientId={},重传门诊商保诊断信息上传支付宝响应参数 response ={}", new Object[]{unuploadMedicalOutpatientDiagnosis.getInpatientId(),JsonUtils.toJsonString(response)});
                        if (response.isSuccess()) {
                            logger.info("inPatientId={},重传门诊商保诊断信息上传成功",unuploadMedicalOutpatientDiagnosis.getInpatientId());
                            unuploadMedicalOutpatientDiagnosis.setUploadFlag(new Short("1"));
                        } else {
                            logger.info("门诊商保诊断信息上传失败");
                            unuploadMedicalOutpatientDiagnosis.setExceptionMessage(response.getSubMsg());
                        }
                        unuploadMedicalOutpatientDiagnosis.setUploadTimes(unuploadMedicalOutpatientDiagnosis.getUploadTimes() + 1);
                        medicalInhospitalDiagnosisDao.update(unuploadMedicalOutpatientDiagnosis);
                    } catch (Exception e) {
                        logger.error("门诊商保诊断信息上传系统异常", e);
                    }
                }
            }
        } catch (Exception e) {
            logger.error("门诊商保诊断信息上传系统异常", e);
        }
    }

    public void executeUploadMedicalInhospitalFeeMessage(List<MedicalInhospitalFee> medicalInhospitalFeeList) {
        try {
            for (MedicalInhospitalFee medicalInhospitalFee : medicalInhospitalFeeList) {
                logger.info("inPatientId={},待重传住院费用信息 medicalInhospitalFee ={}", new Object[]{medicalInhospitalFee.getInpatientId(),JsonUtils.toJsonString(medicalInhospitalFee)});
                MedicalInhospitalFeeVo medicalInhospitalFeeVo = new MedicalInhospitalFeeVo();
                String encryptData = Util.encryptMedicalInhospitalFeeVo(medicalInhospitalFeeVo.buildMedicalInhospitalFeeVo(medicalInhospitalFee), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_INHOSPITAL_FEE);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("inPatientId={},重传住院费用信息上传支付宝响应参数 response ={}", new Object[]{medicalInhospitalFee.getInpatientId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("inPatientId={},重传住院费用信息上传成功",medicalInhospitalFee.getInpatientId());
                        medicalInhospitalFee.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("住院费用信息上传失败");
                        medicalInhospitalFee.setExceptionMessage(response.getSubMsg());
                    }
                    medicalInhospitalFee.setUploadTimes(medicalInhospitalFee.getUploadTimes() + 1);
                    medicalInhospitalFeeDao.update(medicalInhospitalFee);

                } catch (Exception e) {
                    logger.error("住院费用信息上传系统异常", e);
                }
            }

        } catch (Exception e) {
            logger.error("住院费用信息上传系统异常", e);
        }
    }


    public void executeUploadMedicalInhospitalFeeDetailMessage(List<MedicalInhospitalFeeDetail> medicalInhospitalFeeDetailList) {
        try {
            for (MedicalInhospitalFeeDetail medicalInhospitalFeeDetail : medicalInhospitalFeeDetailList) {
                logger.info("inPatientId={},待重传住院费用明细信息medicalInhospitalFeeDetail ={}", new Object[]{medicalInhospitalFeeDetail.getInpatientId(),JsonUtils.toJsonString(medicalInhospitalFeeDetail)});
                MedicalInhospitalFeeDetailVo medicalInhospitalFeeDetailVo = new MedicalInhospitalFeeDetailVo();
                String encryptData = Util.encryptMedicalInhospitalFeeDetailVo(medicalInhospitalFeeDetailVo.buildMedicalInhospitalFeeDetailVo(medicalInhospitalFeeDetail), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_INHOSPITAL_FEE_DETAIL);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("inPatientId={},重传住院费用明细信息上传支付宝响应参数 response ={}", new Object[]{medicalInhospitalFeeDetail.getInpatientId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("inPatientId={},重传住院费用明细信息上传成功",medicalInhospitalFeeDetail.getInpatientId());
                        medicalInhospitalFeeDetail.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("住院费用明细信息上传失败");
                        medicalInhospitalFeeDetail.setExceptionMessage(response.getSubMsg());
                    }
                    medicalInhospitalFeeDetail.setUploadTimes(medicalInhospitalFeeDetail.getUploadTimes() + 1);
                    medicalInhospitalFeeDetailDao.update(medicalInhospitalFeeDetail);
                } catch (Exception e) {
                    logger.error("住院费用明细信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("住院费用明细信息上传系统异常", e);
        }
    }

    public void executeUploadMedicalInvoiceNoMessage(List<MedicalInvoiceNo> medicalInvoiceNoList) {
        try {
            for (MedicalInvoiceNo medicalInvoiceNo : medicalInvoiceNoList) {
                logger.info("待重传发票编号补传信息 medicalInvoiceNo ={}", new Object[]{JsonUtils.toJsonString(medicalInvoiceNo)});
                String encryptData = Util.encryptMedicalInvoiceNo(medicalInvoiceNo, Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_INVOICE_SUPPLY);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("发票编号补传信息上传支付宝响应参数 data ={}", new Object[]{JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("发票编号补传信息上传成功");
                        medicalInvoiceNo.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("发票编号补传信息上传失败");
                        medicalInvoiceNo.setExceptionMessage(response.getSubMsg());
                    }
                    medicalInvoiceNo.setUploadTimes(medicalInvoiceNo.getUploadTimes() + 1);
                    medicalInvoiceNoDao.update(medicalInvoiceNo);
                } catch (Exception e) {
                    logger.error("发票编号补传信息上传系统异常", e);
                }
            }
        } catch (Exception e) {
            logger.error("发票编号补传信息上传系统异常", e);
        }
    }


    /**
     * 测试用代码
     * @param data
     * @return
     */
    public Response updateNotifyPatientStatus(String data){
        logger.info("更新就诊人信息状态入参 data={}",data);
        Response response = new Response();
        MedicalPatient medicalPatient= JsonUtils.parseObject(data,MedicalPatient.class);
        if(medicalPatient.getBusinessType() == 1){
            medicalOutpatientDiagnosisDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalOutpatientFeeDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalOutpatientFeeDetailDao.deleteByMedicalPatientId(medicalPatient.getId());

            medicalCheckingDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalCheckingDetailDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalExaminationDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalOperationRecordDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalPatientDao.update(medicalPatient);
        }else {
//            medicalInhospitalDiagnosisDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalInhospitalFeeDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalInhospitalFeeDetailDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalInhospitalReportidsDao.deleteByMedicalPatientId(medicalPatient.getId());

            medicalCheckingDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalCheckingDetailDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalExaminationDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalOperationRecordDao.deleteByMedicalPatientId(medicalPatient.getId());
            medicalPatientDao.update(medicalPatient);
        }

        response.setResultCodeAndMessage("0","成功");
        return response;
    }

    /**
     * 测试用代码
     * @param data
     * @return
     */
    public Response deleteNotifyPatientStatus(String data){
        logger.info("删除住院诊断入参 data={}",data);
        Response response = new Response();
        MedicalInhospitalDiagnosis medicalInhospitalDiagnosis = JsonUtils.parseObject(data,MedicalInhospitalDiagnosis.class);
        medicalInhospitalDiagnosisDao.delete(medicalInhospitalDiagnosis);
        response.setResultCodeAndMessage("0","成功");
        return response;
    }




    public Short converterStringToShort(String value) {
        return value == null ? null : Short.valueOf(value);
    }

    public Integer converterStringToInt(String value) {
        return value == null ? null : converterStringToInt(value);
    }

    private Short conveterStringToShort(String value) {
        return value == null ? null : Short.valueOf(value);
    }

    private Integer conveterStringToInt(String value) {
        return value == null ? null : Integer.valueOf(value);
    }
}
